본문 바로가기
Programming Languages/Python

Chapter 8. 문자열

by 더 이프 2023. 3. 16.
728x90

Chapter 8. 문자열

 1. 문자열

  ⦁ 문자열이란 문자, 단어 등으로 구성된 문자들의 집합을 의미

  ⦁ \를 출력하기 위해서는 \앞에 \를 한번 더 사용하여 출력

 a. string

python
닫기
# 문자열 s = "abc\\defg" print(s) # 결과값 abc\defg

 2. 문자열 처리

  ⦁ format과 중괄호를 사용해 중괄호 안에 변수를 넣을 수 있으며 순차적으로 들어감

  ⦁ split의 기본값은 빈칸을 기준으로 자르지만 매개변수를 입력하면 매개변수를 기준으로 자름

  ⦁ lstrip은 문자 앞의 공백은 취급하지 않으며, 선택한 문자 중 왼쪽에 있는 문자만 제거하고 나머지는 남아 있음

 a. format

python
닫기
# format a = 10 b = 'def' # format시 중괄호안에 변수를 넣을 수 있으며 순차적으로 들어감 s = "head {} tail {}".format(b,a) print(s) # 결과값 head def tail 10

 b. split

python
닫기
# split s = 'Return a list of the substrings in the string, using sep as the separator string.' print(s.split(',')) # 결과값 ['Return a list of the substrings in the string', ' using sep as the separator string.']

 c. lstrip

python
닫기
# lstrip s = '....x....abc' #lstrip은 문자 앞의 공백은 취급하지 않음 #왼쪽에 있는 문자만 제거하고 중간에는 남아 있음 print(s.lstrip('.')) # 결과값 x....abc