In [1]:
import string
문자열 상수
In [5]:
string.ascii_letters
Out[5]:
In [3]:
string.ascii_lowercase
Out[3]:
In [4]:
string.ascii_uppercase
Out[4]:
In [6]:
string.digits
Out[6]:
In [8]:
digits = ''
for n in range(10):
digits += str(n)
print(digits)
In [12]:
print('{}, {}'.format('hello', 'world'))
In [11]:
print(u'{}, {}'.format(u'안녕', u'세계'))
In [13]:
print('{:,}'.format(1000000000000000))
In [20]:
n = 1000000000000000
# 세 번마다 쉼표 삽입
n_comma = ''
for i, n_str in enumerate(str(n)[::-1]):
#print(i, n_str)
n_comma += str(n_str)
if i%3 == 0:
n_comma += ','
print(n_comma[::-1])
In [21]:
from string import Template
In [24]:
template = Template('$who likes $what')
template.substitute(who='SJ', what='Python')
Out[24]:
In [25]:
text = 'Hello, World!'
In [26]:
text.split()
Out[26]:
In [27]:
text.split(',')
Out[27]:
In [29]:
string.capwords('HelloWorld')
Out[29]:
문자열 검색
In [30]:
text = 'big python, python for big data'
In [31]:
string.find(text, 'python')
Out[31]:
In [38]:
string.find(text, 'python', 10)
Out[38]:
In [33]:
text[4]
Out[33]:
In [34]:
string.rfind(text, 'python')
Out[34]:
In [35]:
text[12]
Out[35]:
In [37]:
if string.find(text, 'python') == string.rfind(text, 'python'):
print('유일한 단어')
else:
print('해당 단어가 두 개 이상')
In [63]:
text = 'big python, python for big data. Python is the best!'
text_kr = u'빅 파이썬, 빅데이터를 위한 파이썬. 파이썬이 최고'
In [39]:
string.lower('HELLO')
Out[39]:
In [46]:
string.find(text, 'javascript')
Out[46]:
In [65]:
string.find(text_kr, u'파이썬')
Out[65]:
In [49]:
text = 'big python, python for big data. Python is the best!'
string.find(text, 'python', 18)
Out[49]:
In [53]:
text = 'big python, python for big data. Python is the best!'
text_lower = string.lower(text)
start_index_list = []
start_idx = 0
while start_idx < len(text_lower):
idx = string.find(text_lower, 'python', start_idx)
if idx == -1:
break
start_index_list.append(idx)
start_idx = idx+len('python')
print(start_index_list)
In [55]:
text = 'big python, python for big data. Python is the best!'
text.lower().count('python')
Out[55]:
In [56]:
string.replace(text, 'python', 'javascript')
Out[56]:
In [58]:
string.replace(text, 'python', '')
Out[58]:
In [59]:
string.replace(text, 'python', '', 1)
Out[59]:
해결하고자 하는 문제: 텍스트에서 특정한 패턴의 탐색, 추출
In [109]:
text = """이성주의 전화번호는 010-1234-5678입니다.
이메일은 seongjoo@codebasic.co입니다. 김성주의 전화번호는 010-4321-8765입니다."""
In [113]:
import re
phonePattern = re.compile(r'\d{3}-\d{4}-\d{4}')
phonePattern.findall(text)
Out[113]:
In [ ]: