#문자열 내 작은따옴표 #큰따옴표로 문자열 둘러싸기
>>> print("Jungmin's username is Sayho.")
Jungmin's username is Sayho.
#문자열 내 큰따옴표 #작은따옴표로 문자열 둘러싸기
>>> print('"How are you?" Sayho says.')
"How are you?" Sayho says.
>>> print('Jungmin\'s username is Sayho.')
Jungmin's username is Sayho.
>>> print("\"How are you?\" Sayho says.")
"How are you?" Sayho says.
>>> print("I have %d hotdogs." % 5) #정수 대입
I have 5 hotdogs.
>>> print("I have %s hotdogs." % "five") #문자열 대입
I have five hotdogs.
>>> number = 5
>>> print("I have %s hotdogs." % number) #변수 대입
I have 5 hotdogs.
>>> number = ten
>>> weight = 3
>>> print("I ate %s hotdogs, so I gained %d kilos." % (number, weight))
I ate ten hotdogs, so I gained 3 kilos.
# %s를 사용하면,어떤 형태의 값이든 문자열의 형태로 변환 가능
>>> print("I have %s hotdogs." % 5) #문자열 대입
I have 5 hotdogs.
>>> print("Error is %d%." % 98)
ERROR
>>> print("Error is %d%%." % 98)
Error is 98%. # % 출력 위해 %% 씀
#길이 10인 문자열, 오른쪽정렬
>>> print("%10s" % "sayho")
' sayho'
#포맷문자열길이 10, 왼쪽정렬
>>> print("%-10shi" % "sayho")
'sayho hi'
#소수점 네번째 자리까지
>>> print("%0.4f" % 3.42134234)
'3.4213'
#문자열길이 10, 소수점 네번째 자리까지
>>> print("%10.4f" % 3.42134234)
' 3.4213'
>>> print("I have {0} hotdogs.".format(5))
'I have 5 hotdogs.'
>>> print("I have {0} hotdogs.".format("five"))
'I have five hotdogs.'
>>> number = 5
>>> print("I have {0} hotdogs.".format(number))
'I have 5 hotdogs.'
>>> number = ten
>>> weight = 3
>>> print("I ate {0} hotdogs, so I gained {1} kilos.".format(number, weight))
I ate ten hotdogs, so I gained 3 kilos.
>>> print("I ate {number} hotdogs, so I gained {weight} kilos.".format(number=10, weight=3))
I ate ten hotdogs, so I gained 3 kilos.
#길이 10인 문자열, 왼쪽정렬(:<)
>>> print("{0:<10}".format("sayho") )
'sayho '
#길이 10인 문자열, 왼쪽정렬(:>)
>>> print("{0:>10}".format("sayho") )
' sayho'
#길이 11인 문자열, 가운데정렬(:^)
>>> print("{0:^11}".format("sayho") )
' sayho '
>>> print("{0:=^11}".format("sayho") ) # :와 ^ 사이에 공백문자 넣기
'===sayho==='
>>> print("{0:0.4f}".format(3.42134234))
'3.4213'
>>> print("{0:10.4f}".format(3.42134234))
' 3.4213'
>>> d = {'number': 'ten', 'weight': 3}
>>> print(f"I ate {d['number']} hotdogs, so I gained {d['weight']} kilos.")
I ate ten hotdogs, so I gained 3 kilos.
a.count('b')
문자열 a 중 문자 b의 개수
a.find('b')
문자열 a 중 문자 b가 처음으로 나온 위치 #존재하지 않을 시 -1
a.index('b')
문자열 a 중 문자 b가 처음으로 나온 위치 #존재하지 않을 시 error
a.join(b)
문자 a를 문자열(혹은 리스트, 튜플) b의 각 문자 사이에 삽입
a.upper()
a를 대문자로
a.lower()
a를 소문자로
a.lstrip()
a의 왼쪽 연속공백 삭제
a.rstrip()
a의 오른쪽 연속공백 삭제
a.strip()
연속공백 모두 삭제
a.replace('b', 'c')
문자열 a 중 b부분을 c로 치환
a.split('b')
문자열 a를 문자 b를 기준으로 나눔 #보통 공백, : 등
아래 내용을 바탕으로 정리한 것이다.
점프투파이썬 (https://wikidocs.net/13)