개발하고 싶어요
문자열 응용하기 본문
In [28]:
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity="all"
문자열 조작하기¶
문자열 바꾸기¶
- replace('바꿀 문자열', '새 문자열')
In [2]:
s = 'hello, world'
s = s.replace('world', 'python')
s
Out[2]:
'hello, python'
문자 바꾸기¶
- str.maketrans('바꿀 문자', '새 문자')
In [3]:
# a는 1 e는 2 i는 3 o는 4 u는 5로 변경
table = str.maketrans("aeiou", "12345")
print('apple'.translate(table))
1ppl2
문자열 분리하기¶
- split('기준 문자열')
In [4]:
"apple pear grape pineapple orange".split()
Out[4]:
['apple', 'pear', 'grape', 'pineapple', 'orange']
In [6]:
"apple, pear, grape, pineapple, orange".split(", ")
Out[6]:
['apple', 'pear', 'grape', 'pineapple', 'orange']
구분자 문자열과 문자열 리스트 연결¶
- join(리스트)
In [8]:
' '.join(['apple', 'pear', 'grape', 'pineapple', 'orange'])
Out[8]:
'apple pear grape pineapple orange'
In [9]:
'-'.join(['apple', 'pear', 'grape', 'pineapple', 'orange'])
Out[9]:
'apple-pear-grape-pineapple-orange'
소문자를 대문자로 변경¶
- upper()
In [10]:
'pthon'.upper()
Out[10]:
'PTHON'
In [11]:
'Hello'.upper()
Out[11]:
'HELLO'
대문자를 소문자로 변경¶
- lower()
In [12]:
'PYTHON'.lower()
Out[12]:
'python'
In [13]:
'hELLO'.lower()
Out[13]:
'hello'
왼쪽 공백 삭제¶
- lstrip()
In [14]:
" python ".lstrip()
Out[14]:
'python '
오른쪽 공백 삭제¶
- rstrip()
In [15]:
" python ".rstrip()
Out[15]:
' python'
양쪽 공백 삭제¶
- strip()
In [16]:
" python ".strip()
Out[16]:
'python'
왼쪽의 특정 문자 삭제¶
- lstrip('삭제할 문자들')
In [17]:
", python.".lstrip(',.')
Out[17]:
' python.'
In [18]:
", python.".lstrip(',. ')
Out[18]:
'python.'
오른쪽의 특정 문자 삭제¶
- rstrip('삭제할 문자들')
In [19]:
", python.".rstrip(',.')
Out[19]:
', python'
양쪽의 특정 문자 삭제하기¶
- strip('삭제할 문자들')
In [20]:
", python.".strip(',.')
Out[20]:
' python'
문자열 왼쪽 정렬하기¶
- ljust(길이) : 문자열을 지정된 길이로 만든 뒤 왼쪽으로 정렬 후 남는 공간을 공백으로
In [21]:
'python'.ljust(10)
Out[21]:
'python '
문자열을 오른쪽 정렬¶
- rjust(길이)
In [23]:
'python'.rjust(10)
Out[23]:
' python'
문자열을 가운데 정렬¶
- centet(길이)
In [24]:
'python'.center(10)
Out[24]:
' python '
문자열 왼쪽에 0채우기¶
- zfill(길이)
In [25]:
'3.5'.zfill(5)
Out[25]:
'003.5'
In [26]:
'hello'.zfill(7)
Out[26]:
'00hello'
문자열 위치 찾기¶
- find('찾을 문자열')
- 문자열이 없으면 -1
In [27]:
"apple pineapple".find('xy')
Out[27]:
-1
In [29]:
"apple pineapple".find('pl')
Out[29]:
2
오른쪽부터 문자열 위치 찾기¶
- rfind('찾을 문자열')
In [30]:
"apple pineapple".rfind('xy')
Out[30]:
-1
In [33]:
"apple pineapple".rfind('pi')
Out[33]:
6
문자열 위치 찾기¶
- index('찾을 문자열')
In [34]:
"apple pineapple".index('pi')
Out[34]:
6
오른쪽에서부터 문자열 위치 찾기¶
- rindex('찾을 문자열')
In [35]:
"apple pineapple".rindex('pl')
Out[35]:
12
문자열 개수 세기¶
- count('문자열')
In [36]:
"apple pineapple".count('pl')
Out[36]:
2
문자열 서식 지정자와 포매팅 사용하기¶
서식 지정자로 문자열 넣기¶
- "%s" % "문자열"
In [2]:
'I am %s." % "james'
Out[2]:
'I am %s." % "james'
In [3]:
name ="maria"
"I am %s." % name
Out[3]:
'I am maria.'
서식 지정자로 숫자 넣기¶
- "%d" % 숫자
In [5]:
"I am %d years old." % 24
Out[5]:
'I am 24 years old.'
서식 지정자로 소수점 표현하기¶
- "%f" % 숫자
- "%.자릿수f" % 숫자
In [6]:
"%f" % 5.3
Out[6]:
'5.300000'
In [7]:
"%.3f" % 5.3
Out[7]:
'5.300'
서식 지정자로 문자열 정렬하기¶
- "%길이s" % 문자열 : 오른쪽 정렬 후 남은 공간은 공백으로
- "%-길이s" % 문자열 : 왼쪽으로 정렬 후 남은 공간은 공백으로
- "%길이.자릿수f" % 숫자 : .(점) 앞에 정렬할 길이를 지정하고, 점 뒤에 소수점 이하 자릿수를 지정
In [8]:
"%10s" % "python"
Out[8]:
' python'
In [9]:
"%8.3f" % 4.5
Out[9]:
' 4.500'
In [10]:
"%8.3f" % 400.5
Out[10]:
' 400.500'
서식 지정자로 문자열 안에 값 여러 개 넣기¶
- "%d %s" % (숫자, "문자열")
In [11]:
"Today is %d %s." % (3, "April")
Out[11]:
'Today is 3 April.'
In [12]:
"Today is %d%s." % (3, "April")
Out[12]:
'Today is 3April.'
format메서드 사용하기¶
- "{인덱스}".format(값)
In [13]:
"hello, {0}".format("world")
Out[13]:
'hello, world'
In [15]:
"hello, {0}".format(100)
Out[15]:
'hello, 100'
format메서드로 값을 여러 개 넣기¶
In [16]:
"hello, {0} {1} {2}".format("python", 'Script', 3.6)
Out[16]:
'hello, python Script 3.6'
format메서드로 같은 값을 여러 개 넣기¶
In [17]:
"{0} {1} {1} {0}".format("python", 'Script')
Out[17]:
'python Script Script python'
format메서드에서 인덱스 생략하기¶
In [18]:
"hello, {} {} {}".format("python", "script", 3.6)
Out[18]:
'hello, python script 3.6'
format메서드에서 인덱스 대신 이름 지정하기¶
In [19]:
"hello, {language} {version}".format(language = "python", version = 3.6)
Out[19]:
'hello, python 3.6'
문자열 포멧팅에 변수를 그대로 사용¶
In [20]:
lang = "python"
ver = 3.6
f"hello, {lang} {ver}"
Out[20]:
'hello, python 3.6'
format메서드로 문자열 정렬¶
- "{인덱스 : < 길이}".format(값)
In [21]:
"{0:<10}".format("python") # 왼쪽 정렬
Out[21]:
'python '
숫자 개수 맞추기¶
- "%0개수d" % 숫자
- "{인덱스 : 0개수d}".format(숫자)
In [22]:
"%03d" % 5
Out[22]:
'005'
In [23]:
"{0:03d}".format(5)
Out[23]:
'005'
- "%0개수.자릿수f" % 숫자
- "{인덱스 : 0개수.자릿수f}".format(숫자)
In [24]:
"%03.3f" % 4.5
Out[24]:
'4.500'
In [25]:
"{0:03.3f}".format(4.5)
Out[25]:
'4.500'
채우기와 정렬을 조합해서 사용하기¶
- "{인덱스 : [[채우기]정렬][길이][.자릿수][자료형]}"
In [32]:
print("{0:0<10}".format(15)) # 길이 10, 왼쪽으로 정렬하고 남는 공간은 0으로
print("{0:0>10}".format(15)) # 길이 10, 오른쪽으로 정렬하고 남는 공간은 0으로
1500000000
0000000015
In [33]:
"{0:0>10.2f}".format(15) # 길이 10, 오른쪽으로 정렬하고 소수점 자릿수는 2자리
Out[33]:
'0000015.00'
In [34]:
print("{0: >10}".format(15)) # 남는 공간을 공백으로 채움
print("{0:x>10}".format(15)) # 남는 공간을 x로 채움
15
xxxxxxxx15
'PYTHON' 카테고리의 다른 글
set (0) | 2023.12.26 |
---|---|
딕셔너리 응용하기 (1) | 2023.12.26 |
리스트, 튜플 응용 (0) | 2023.12.26 |
2차원 리스트 사용하기 (0) | 2023.12.26 |
별 그리기 (0) | 2023.12.26 |