-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringType.py
More file actions
154 lines (118 loc) · 3.57 KB
/
Copy pathstringType.py
File metadata and controls
154 lines (118 loc) · 3.57 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# 문자열 타입
# 파이썬의 문자열은 변경 불가능한 자료형임
# "", '', """, '''
print("[ 문자열 출력 ]\n")
print("Hello World")
print('Python is fun')
print("""Life is too short, You need python""")
print('''Life is too short, You need python''')
print("=" * 50)
# ""나 ''로도 문자열 타입을 생성할 수 있지만, 멀티라인을 입력하거나 문자열 내 " 나 '를 문자열 선언형으로 인식하지 않도록 """ , ''' 를 사용할 수 있다.
print("""my friend said, "welcome to python world" """)
print('''It's mine''')
multiline = """Life is too short
You need python"""
print(multiline)
# """ 이후 줄바꿈(엔터 입력) 시 표시 되는 문자열 또한 줄이 바뀐 문자열이 표시됨
multiline = """
Life is too short
You need python
"""
print(multiline)
print("=" * 50)
# 작은 따옴표 내 큰 따옴표, 큰 따옴표 내 작은 따옴표는 문자열로 인식된다 (이스케이프 코드를 이용해서도 표현 가능)
print("Python's favorite food is perl")
print('"Python is very easy." he says')
print("=" * 50)
print("[ 문자열 연산 ]\n")
# 문자열 연산
head = "Python"
tail = ' is fun!'
print(head + tail)
a = 'python'
print(a * 2)
print("=" * 50)
print("[ 문자열 길이 ]\n")
# 문자열 길이(공백도 포함됨)
a = "Life is too short"
print(len(a))
a = "Life is too short "
print(len(a))
# 문자열 인덱싱
print(a[3])
if a[-1] == ' ':
print("공백문자열")
else:
print(a[-1])
print("=" * 50)
print("[ 문자열 인덱싱과 슬라이싱 ]\n")
# 문자열 슬라이싱
a = "Life is too short, You need Python"
b = a[0] + a[1] + a[2] + a[3]
print(b)
print(a[0:4])
print(a[5:])
print(a[:17])
print(a[19:-7])
print("=" * 50)
print("[ 문자열 포매팅 ]\n")
# 문자열 포매팅
print("I eat %s apples" % 3)
print("I eat %s apples" % "five")
print("I ate %d apples. So I was sick for %s days." % (10, "five"))
print("Error is %d%%" % 98)
# 정렬과 공백
print("%10sJane" % "hi")
print("%-10sJane" % "hi")
print("{0:<10}".format("hi"))
print("{0:>10}".format("hi"))
print("{0:^10}".format("hi"))
print("{0:=^10}".format("hi"))
print("{0:!<10}".format("hi"))
# 소수점 표현
print("%0.4f" % 3.42134234)
# format 함수를 이용한 문자열 포맷
print("I eat {0} apples".format(3))
print("I eat {0} apples".format("five"))
print("I ate {0} apples. So I was sick for {1} days".format(10, "three"))
print("I ate {number} apples. So I was sick for {day} days".format(number=10, day=3))
print("I ate {0} apples. So I was sick for {day} days".format(10, day=3))
print("{{ and }}".format()) # {}와 같은 중괄호 문자는 포매팅을 위한 문자이므로 연속해서 사용 시 표시됨
# f 문자열 포매팅
name = "서성민"
print(f"나의 이름은 {name} 입니다.")
print(f"{'hi':>10}")
print("=" * 50)
print("[ 문자열 함수 ]\n")
# count 함수
a = "hobby"
print(a.count("b"))
# find 함수
a = "Python is the best choice"
print(a.find("b"))
print(a.find("k"))
# index 함수
a = "Life is too short"
print(a.index('t'))
# print(a.index('k')) -- index 함수는 찾을 대상이 문자열에 존재하지 않는다면 오류를 발생시킴
# join 함수
print(",".join('abcd'))
# upper 함수
print("hi".upper())
a = "hI bYe"
print(a.upper())
# lower 함수
print(a.lower())
# lstrip, rstrip, strip 함수
a = "{0:^5}".format("hi")
print(a.lstrip())
print(a.rstrip())
print(a.strip())
# replace 함수
a = "Life is too short"
print(a.replace("Life", "Your leg"))
# split 함수 -- 리스트 형으로 리턴
a = "Life is too short"
print(a.split())
b = "a:b:c:d"
print(b.split(":"))