[Python]문자형, 문자함수
파이썬에서의 문자형 변수와 문자함수
# 문자열 생성 str1 = "I am Emperor" str2 = 'Python' str3 = """따옴표 3개도 문자열 선언가능""" str4 = ''' Im fine, Thank you''' print(type(str1), type(str2), type(str3), type(str4)) print(len(str1), len(str2), len(str3), len(str4)) # 길이 # 빈 문자열 str1_t1 = '' str2_t2 = str() print(type(str1_t1), len(str1_t1)) print(type(str2_t2), len(str2_t2)) # Escape 문자사용 print("I'am boy") print("I\"am boy") print('I\'am boy') # 탭, 줄바뀸 print('a \t b') print('c \n d') escape_str1 = "Do you have a \"money\"?" print(escape_str1) escape_str2 = 'Do you have a \'money\'?' print(escape_str2) # Raw String : 입력된 문자 그대로 출력 raw_s1 = r'D:\python\test' # 소문자 r print(raw_s1) # 멀티라인 입력 : 문장의 마지막에 \ 을 붙인다. multi_str = \ """ 안녕하세요. 여러줄 문자를 입력해요. 글자 표시 """ print(multi_str) print() # 문자열의 연산 str_o1 = "apple" str_o2 = "Samsung" str_o3 = "서울 대전 대구 부산" str_o4 = "jigi" print(str_o1 * 3) print(str_o1 + str_o2) print('서울' in str_o3) print('제주' in str_o3) print('제주' not in str_o3) print() # 문자열 형변환 print(str(20), type(str(20))) print(str(10.5), type(str(10.5))) print(str(True), type(str(True))) print() # 문자열 함수(upper, isalnum, startswith, count, endswith, isalpha...) print("Capitalize : ", str_o1.capitalize()) print("startsWith : ", str_o2.startswith("S")) print("endsWith : ", str_o2.endswith("g")) print("replace : ", str_o1.replace("p", "t")) print("Sorted : ", sorted("bcda")) print("Split : ", str_o3.split(" ")) # 반복(시퀀스) im_str = "Great Man!" print(dir(im_str)) # dir 내장함수는 인자로 넘겨진 객체가 가지고 있는 변수와 메서드를 배열로 반환한다. # 출력 for i in im_str: print(i) # 쪼개기(Slicing) 연습 str_s1 = "Nice Python" print(len(str_s1)) print(str_s1[0:3]) # 0 ~ 2까지만 나옴 print(str_s1[5:12]) print(str_s1[5:]) # 5 ~ 마지막 글자 print(str_s1[:]) # 모든글자 print(str_s1[1:4:2]) # 1 ~ 3까지 2글자씩 건너뛰며 가져온다. print(str_s1[-5:]) # 오른쪽 자리서 5자리를 가져온다. print(str_s1[1:-2]) # 둘째 자리에서 오른쪽 2자리 전까지 가져온다. print(str_s1[::2]) # 처음부터 끝까지, 2자리씩 건너 뛰어 가져온다. print(str_s1[::-1]) # 처음부터 끝까지, 오른쪽 자리부터 1자리씩 건너뛰어 가져온다. ( reverse ) print() # 아스키 코드 a = 'z' print(ord(a)) # 아스키 코드값 출력 print(chr(122)) # 아스키 코드값으로 문자열 출력
결과 :
<class 'str'> <class 'str'> <class 'str'> <class 'str'> 12 6 16 19 <class 'str'> 0 <class 'str'> 0 I'am boy I"am boy I'am boy a b c d Do you have a "money"? Do you have a 'money'? D:\python\test 안녕하세요. 여러줄 문자를 입력해요. 글자 표시 appleappleapple appleSamsung True False True 20 <class 'str'> 10.5 <class 'str'> True <class 'str'> Capitalize : Apple startsWith : True endsWith : True replace : attle Sorted : ['a', 'b', 'c', 'd'] Split : ['서울', '대전', '대구', '부산'] ['__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 r e a t M a n ! 11 Nic Python Python Nice Python ie ython ice Pyth Nc yhn nohtyP eciN 122 z
최신 댓글