1. 문자열의 길이 : len()
len(str)
2. 공백 제거 : strip()
str2 = ' Python is easy and simple'
print(str2)
print(str2.strip())
# Python is easy and simple 출력
#Python is easy and simple 출력
3. 특정 문자를 다른 문자로 대체 : replace()
msg1 = 'Python is easy and simple'
print(msg1)
print(msg1.replace('Python','Java'))
# 실제로 반영되진 않음
print(msg1)
# 반영하고 싶다면 변수에 담아줘야 한다.
msg1 = msg1.replace('Python','Java')
4. 문자열 나누기 : split()
msg2 = 'Python is easy and simple'
print(msg2)
print(msg2.split())
#공백을 기준으로 나뉨
#list라는 자료형으로 저장
msg3 = 'Python_is_easy_and_simple'
print(msg3)
print(msg3.split())
#default는 공백이기 때문에 나눠지지 않았다
print(msg3.split('_'))
5. 소문자로, 대문자로 : lower(), upper()
print(msg2)
# 문자열을 소문자로 바꿔줌 : lower()
print(msg2.lower())
# 문자열을 대문자로 바꿔줌 : upper()
print(msg2.upper())
6. 문자열에서 특정 문자가 몇 개 존재하는지 : count()
msg1.count('P')
# msg1은 'Python is easy and simple'이기 때문에
# 1 출력
7. 특정 문자가 어떤 인덱스에 존재하는지 : find(), index()
msg1.find('y')
# 'y'가 위치한 자리 중 첫번째 인덱스를 출력
# 1 출력
msg1.index('y')
# find와 같이 나옴
※ find(), index() 차이점
msg1.find('안녕')
#없는 문자에 -1
msg1.index('안녕')
#없는 문자에 오류
[Python] 연산자 (0) | 2021.12.29 |
---|---|
[Python] 키보드로 입력 받는 함수: input() (0) | 2021.12.29 |
[Python] 기초 (2) 문자열 포매팅(formatting) (0) | 2021.12.21 |
[Python] 기초 (1) 변수와 문자열, 인덱싱과 슬라이싱 (0) | 2021.12.21 |
[Phython] Jupyter Notebook 이용하기 (0) | 2021.12.21 |