program/python

[python] strip

momoa210 2024. 1. 28. 17:08

strip : 문자열의 중간 공백은 나두고 앞 뒤의 공백, \t, \n 을 모두 지워줌 
 
 
str.strip()

선행과 후행 문자가 제거된 문자열의 복사본을 돌려줍니다. chars 인자는 제거할 문자 집합을 지정하는 문자열입니다. 생략되거나 None 이라면, chars 인자의 기본값은 공백을 제거하도록 합니다. chars 인자는 접두사가 아닙니다. 모든 값 조합이 제거 됩니다.
str.strip([chars])


>>> ex_str = "                   hello         "
>>> ex_str.strip()


# 'hello'

>>>  'http://www.example.com'.strip('m') 


# 'http://www.example.co'

>>> 'http://www.example.com'.strip('w')


# '.example.com'

>>> 'http://www.example.com'.strip('cmowz.') 


# 'example'


lstrip()_ 선행문자만 지울 때 사용함

>>> url = 'https://wikidocs.net'

# strip() 을 사용했을 때, net 의 't'도 생략됨.
>>> url.strip('https://')


# 'wikidocs.ne'



# lstrip() 을 사용했을 때, 
>>> url.lstrip('https://')


# 'wikidocs.net'





rstrip()_ 후행문자만 지울 때 사용함

>>> url = 'https://wikidocs.net'
>>> url.rstrip('.net')


# 'https://wikidocs'

'program > python' 카테고리의 다른 글

[python] 파일 쓰기  (0) 2024.01.28
[python] text 파일 읽기  (0) 2024.01.28
[python] 리스트와 문자열  (0) 2024.01.28
[python] aliasing  (0) 2024.01.28
[python] 사전 dictionary  (0) 2024.01.28