[Python] 문자열 자료형

Jungmin Seo·2021년 5월 15일
0

1. 문자열에 큰따옴표, 작은따옴표 포함시키기

1-1 서로 다른 따옴표 사용하기

문자열 내에 없는 따옴표 사용하기

#문자열 내 작은따옴표 #큰따옴표로 문자열 둘러싸기
>>> print("Jungmin's username is Sayho.")
Jungmin's username is Sayho. 

#문자열 내 큰따옴표 #작은따옴표로 문자열 둘러싸기
>>> print('"How are you?" Sayho says.')
"How are you?" Sayho says. 

1-2 백슬래시 사용

문자열 내 따옴표 앞에 백슬래시 삽입하기

>>> print('Jungmin\'s username is Sayho.')
Jungmin's username is Sayho. 

>>> print("\"How are you?\" Sayho says.")
"How are you?" Sayho says. 

2. 문자열 포매팅

2-1 %

  • 1개 값 넣기
>>> 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.
  • 2개 이상의 값 넣기
>>> 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
# %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'

2-2 format 함수

>>> 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'

2-3 f 문자열 포매팅

  • 딕셔너리 사용
>>> 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.

3. 문자열 관련 함수

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)

profile
Hello World!

0개의 댓글