[Python] 몰랐던 method 정리

영근·2024년 10월 12일
0

Python

목록 보기
1/1
post-thumbnail

String

capitalize()

단어의 첫 글자를 대문자로 바꿔주는 메소드

s = "abcd"
print(s.capitalize())	# 'Abcd'

title()

문장의 첫 글자를 대문자로 바꿔주는 메소드

s1 = "abcd"
print(s1.title())	# 'Abcd'

s2 = "hello world"
print(s2.title()) # 'Hello World'

이 때, 알파벳이 아닌 다른 문자들을 기준으로 단어를 구분한다는 점에 주의해야 한다.

s1 = "a1b2c3"
print(s1.capitalize())   # 'A1b2c3'
print(s1.title())        # 'A1B2C3'

s2 = "abc-def gh"
print(s2.capitalize())   # 'Abc-def gh'
print(s2.title())        # 'Abc-Def Gh
profile
Undefined JS developer

0개의 댓글