파이썬(Python)/코테 준비

[python 파이썬] 코딩테스트 준비 (문자열)

sunning 2022. 5. 7. 10:47
728x90
반응형

코딩테스트를 준비하면서(사실 너무 취미처럼 푼 것이라 준비했다고 하기도 뭐하지만ㅎㅎ)

생각보다 기초적인 지식이 부족한 것을 느꼈다. 그래서 한 번에 볼 수 있게 정리를 하려고 한다!

문자열

1) 문자열 split

A = 'ab.c de.f'
## Case 1 --> ['ab.c', 'de.f']
print(A.split(' '))

## Case 2 --> [['ab','c'],['de','f']]
print([A.split(" ")[i].split(".") for i in range(len(A.split(' ')))])

## Case 3 --> ['ab','c','de','f']
import re
print(re.split('[. ]', A))
print(A.replace('.', ' ').split(' '))

 

 2) 알파벳 및 숫자 불러오기

import string

string.ascii_lowercase ## 알파벳 소문자 호출
string.ascii_uppercase ## 알파벳 대문자 호출
string.ascii_letters   ## 알파벳 대소문자 호출
string.digits          ## 0부터 9까지 숫자 호출​

 

3) 알파벳 대소문자 바꾸기 및 삭제

Aph = 'AbcdEFg'
print(Aph.upper()) ## 모든 알파벳 대문자 변환
print(Aph.lower()) ## 모든 알파벳 소문자 변환

abc = 'i like you.'
print(abc.capitalize()) ## 맨 첫글자만 대문자 변환
# I like you.
print(abc.title()) ## 알파벳 이외의 문자로 나누어져 있는 영단어들의 첫 글자 모두 대문자 변환
# I Like You.

 

4) 문자열 삭제하기

A = ' apple '
print(A.lstrip()) ## lstrip : 문자열에 왼쪽 공백(인자) 제거 - 'apple '
print(A.rstrip()) ## rstrip : 문자열에 오른쪽 공백(인자) 제거 - ' apple'
print(A.strip())  ## strip  : 문자열의 양쪽 공백(인자) 제거 - 'apple'

print(A.lstrip('p')) ## lstrip : 문자열에 왼쪽 공백(인자) 제거 - ' apple '
print(A.rstrip('elp')) ## rstrip : 문자열에 오른쪽 공백(인자) 제거 - ' apple '
print(A.strip('a'))  ## strip  : 문자열의 양쪽 공백(인자) 제거 - ' apple '

A = 'apple'
print(A.lstrip('p')) ## lstrip : 문자열에 왼쪽 공백(인자) 제거 - 'apple'
print(A.rstrip('elp')) ## rstrip : 문자열에 오른쪽 공백(인자) 제거 - ' a'
print(A.strip('a'))  ## strip  : 문자열의 양쪽 공백(인자) 제거 - 'pple'

 

5) 리스트를 문자열로 변경

A = ['lan','gua','ge']
print(''.join(A)) ##language
B = ['I','am','superman']
print(' '.join(B)) ##I am superman
728x90
반응형