Notice
Recent Posts
Recent Comments
Link
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Archives
Today
Total
관리 메뉴

자바나라

[Python 기초] String function 본문

오늘 배운 파이썬

[Python 기초] String function

주데브 2018. 5. 18. 15:07
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# 대소문자 관련 메소드
 
= "i like Python"
 
print(s.upper()) # 모두 대문자
print(s.lower()) # 모두 소문자
print(s.swapcase())  # 대->소 , 소->대문자로  
print(s.capitalize())   # 문장의 첫글자만 대문자로
print(s.title())   # 단어의 첫글자를 대문자로
 
print("===================================")
 
# 검색 관련 메소드
= "I Like Python. I Like Java Also"
print(s.find("Like"))  # 단어 검색, 검색어가 가장 먼저 나온 위치를 잡아내고 첫 글자 L의 index 값을  출력
print(s.find("Like",3)) # index 3 이후 Like 검색 , 구간설정은 (검색어,start,end) 로 가능하고 end 가 문장끝점일 경우 생략가능
print(s.find("JS")) # 검색결과 없으면 -1
print(s.rfind("a",20,24))  # 검색 구간설정 가능
 
print(s.count("Like"))  # 해당 단어가 몇번 나오는지
print(s.rfind("Like"))
 
print("===================================")
 
print(s.index("Like"))  # s.index 는 값이 없으면 오류가 나고 s.find 는 -1를 출력
# print(s.index("kkk"))
print(s.rfind("Like"))
print(s.startswith("I Like"))
print(s.startswith("Like",2))
print(s.endswith("Also"))
print(s.endswith(("o")))
print(s.endswith("Java",0,26))
print(s.endswith("Java",0,25))
 
print("===================================")
 
# 분리와 결합 메소드
= "spam and ham"
= s.split()  # def는 공백으로 잘라주기
print(t)
 
 
= s.split(" and "# 해당 단어로 쪼개기
print(t)
 
s2 = ":".join(t)  # 쪼개진 단어들 사이사이에 삽입
print(s2)
 
print("===================================")
 
s3 =  "one:two:three:four:five"
print(s3.split(":"))
print(s3.split(":",2))   #  ":" 를 기준으로 자르되 왼쪽부터 2개째까지만 잘라라
print(s3.rsplit(":",2))   #  ":" 를 기준으로 자르되 오른쪽부터 2개째까지만 잘라라
 
print("===================================")
 
lines = """1st line
2nd line
3rd line
4th line
"""
print(lines.splitlines())
##print(lines.split("\n"))
 
print("===================================")
 
#판별 메소드
print('1234'.isdigit())
print("한글만".isalpha())
print("한글만123".isalpha())
print("===================================")
print("abcd".islower())
print("ABCD".isupper())
print("ABcD".isupper())
print("===================================")
print("\n\n".isspace())  ##줄바꿈도 공백으로 인식
print("    ".isspace())
print("".isspace())
print("===================================")
# 0으로 채우기(앞에만)
print("20".zfill(10))
print("12345".zfill(10))
 
 
print("===================================")
#정렬 관련 메소드
 
s="king and queen"
print(s.center(60))  # 총 할당하는 칸수 입력
print(s.center(30))
print(s.center(30,'-'))
print(s.ljust(30,'-'))
print(s.rjust(30,'-'))
 
 
 
 
 
cs


Comments