파이썬(python) 문자형 예제 기초
# chapter 03_2
# 파이썬 문자형
# 문자형 중요
# 문자열 생성
str1 = "i am python"
str2 ='python'
str3 = """how old are you"""
str4 = '''i am python'''
print(type(str1), type(str2), type(str3), type(str4))
print(len(str1), len(str2), len(str3), len(str4))
print()
# 빈문자형
str_t1 = ''
str_t2 = str()
print(type(str_t1), len(str_t1))
print(type(str_t2), len(str_t2))
print()
# 이스케이프 문자
print("i \'am a boy")
print("i \\am a boy")
print("a \t b") # Tab 사이즈 만큼 뛰움
print("a \nb") # 줄바꿈
escape_str = " do you \"know\""
print(escape_str)
# Raw String
raw_s1 = r'd:\tpython\test' # 하드디스크 드라이브 경로 표시 할때 많이 사용
print(raw_s1)
print()
# 멀티라인 입력
multi_str = \
"""
문자열
멀티라인 입력
테스트
"""
# 역슬러쉬를 사용해야 좀 더 가독성이 좋음
print(multi_str)
# 문자열 연산
str_o1 = "python"
str_o2 = "apple"
str_o3 = "how old are you"
str_o4 = "aaa bbb ccc ddd"
print(str_o1*3)
print(str_o1 + str_o2)
print('y' in str_o1)
print('z' in str_o1)
print('p' not in str_o2)
print()
# 문자열에 대한 형변환
print(str(66), type(str(66)))
print(str(10.1))
print(str(True), type(str(True)))
# 문자열 함수 (upper, islnum, startswith, count, endwith, isalpha)
print(str_o1.capitalize()) # 첫번째 문자를 대문자로 변환
print(str_o2.endswith("s")) # 마지막 글자가 안에 글자가 맞는지 확인 하는 함수
print(str_o1.replace("thon","Good")) # 문자를 바꾼다
print(sorted(str_o1)) # 문자를 받아서 리스트 형태로 반환한다.
print(str_o4.split()) # 중간에 문자열을 기준으로 나누는 함수
print()
# 반복(시퀀스)
im_str = "good boy"
print(dir(im_str)) # _iter_라는 속성을 확인함 iter는 반복이 가능함을 확인 가능함을
# 출력
for i in im_str:
print(i)
# 슬라이싱
str_s1 ="nice python"
# 슬라이싱 연습
print(str_s1[0:3]) # 0부터 3까지 나오는것이 아니라 0에서 2까지 나오는거임
print(str_s1[5:11])
print(str_s1[5:]) # 마지막 까지 가져와라
print(str_s1[:len(str_s1)])
print(str_s1[:len(str_s1)-1])
print(str_s1[0:3]) # 0부터 3까지 나오는것이 아니라 0에서 2까지 나오는거임
print(str_s1[1:9:2]) # 0부터 9까지 2개 단위로 가져와라
print(str_s1[-5:])
print(str_s1[1:-2])
print(str_s1[::2])
print(str_s1[::-1]) # 역으로 출력하는 방법
print()
# 아스키 코드(또는 유니코드)
a = 'z'
print(ord(a)) # 문자에 대한 아스키 값을 보여주는것
print(chr(122)) # 아스키 코드값을 문자로 보여주는것
결과
[Command: python -u C:\python_basic\chaper03_02.py]
<class 'str'> <class 'str'> <class 'str'> <class 'str'>
11 6 15 11
<class 'str'> 0
<class 'str'> 0
i 'am a boy
i \am a boy
a b
a
b
do you "know"
d:\tpython\test
문자열
멀티라인 입력
테스트
pythonpythonpython
pythonapple
True
False
False
66 <class 'str'>
10.1
True <class 'str'>
Python
False
pyGood
['h', 'n', 'o', 'p', 't', 'y']
['aaa', 'bbb', 'ccc', 'ddd']
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
g
o
o
d
b
o
y
nic
python
python
nice python
nice pytho
nic
iept
ython
ice pyth
nc yhn
nohtyp ecin
122
z
[Finished in 0.177s]