String

Published by onesixx on

https://wikidocs.net/13

https://onesixx.com/string-to-vaiable/

https://onesixx.com/str-vs-repr-string-vs-representation/

Slicing

'"String"s Slicing'[:2]       #''"S'
"String's Slicing"[0:2]       #'St'

'Hello world'[-3:]            #'rld'
'Hello world'[1:-3]           #'ello wo'

'''
Multiline
String's Slicing
'''[9:12]                     #'e\
S'

"""
Multiline
String"s Slicing
"""[-3]                     #'n'
[c for c in 'abc']   # ['a', 'b', 'c']

len('abc' + 'DEF')   # 6     # 'abcDEF'
('abc'*3).count('a') # 3     # 'abcabcabc'


#------ 찾기 ------
'a' in 'abc'        # True
'a' not in 'abc'    # False

'abc'.startswith('b')  # False
'abc'.endswith('c')    # True

#------ 위치 ------ 
'abc'.find('a')      # 0
'abc'.index('a')     # 0

'abc'.find('s')      # -1
'abc'.index('s')     # ValueError: substring not found


#------ 형태바꾸기 ------ 
'abc'.join(['S','I','X','O'])    # 'SabcIabcXabcO'

'abc'.capitalize()   #'Abc'
'abc'.upper()        #'ABC'
'  abc    '.strip()  #'abc'
'abc'.split('b')     #['a', 'c']

'abc'.replace('c','de')   #'abde'
myRule=str.maketrans('bc','xy')
'abc'.translate(myRule)   # 'axy'

'abc'.zfill(6)       # '000abc'

f-string, 포맷 문자열 리터럴.

["%.2d"%i   for i in range(1, 16)]
[f"{i:02d}" for i in range(1, 16)]

# ['01, '02',  '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15']
관련 문장 – Python String
Python의 문자열에서 쉼표 제거
파이썬 방식으로 문자열이 비어 있는지 확인하는 방법
파이썬에서 문자열에서 공백을 제거하는 방법

문자열 합치기

Categories: Python Basic

onesixx

Blog Owner

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x