문자열 연산

Python 정규 표현식

Maria Eugenia Inzaugarat

Data Scientist

대소문자 조정

my_string = "tHis Is a niCe StriNg"
  • 소문자로 변환
print(my_string.lower())
this is a nice string
  • 대문자로 변환
print(my_string.upper())
THIS IS A NICE STRING
Python 정규 표현식

 

my_string = "tHis Is a niCe StriNg"

 

  • 첫 글자를 대문자로 변환
print(my_string.capitalize())
This is a nice string
Python 정규 표현식

분할

my_string = "This string will be split"
  • 문자열을 부분 문자열 리스트로 분할
my_string.split(sep=" ", maxsplit=2)
['This', 'string', 'will be split']
my_string.rsplit(sep=" ", maxsplit=2)
['This string will', 'be', 'split']
Python 정규 표현식

 

my_string = "This string will be split\nin two"

print(my_string)
This string will be split
in two

Python 정규 표현식
  • 줄 경계에서 분할
my_string = "This string will be split\nin two"

 

my_string.splitlines()
['This string will be split', 'in two']
Python 정규 표현식

결합

  • 리스트나 다른 반복 가능 객체의 문자열을 연결

 

my_list = ["this", "would", "be", "a", "string"]
print(" ".join(my_list))
this would be a string
Python 정규 표현식

문자 제거

  • 왼쪽에서 오른쪽으로 문자를 제거: .strip()

 

my_string = " This string will be stripped\n"
my_string.strip()
'This string will be stripped'
Python 정규 표현식

 

my_string = " This string will be stripped\n"
  • 오른쪽 끝에서 문자 제거
my_string.rstrip()
' This string will be stripped'
  • 왼쪽 끝에서 문자 제거
my_string.lstrip()
'This string will be stripped\n'
Python 정규 표현식

연습해 봅시다!

Python 정규 표현식

Preparing Video For Download...