"python"
"123"
"python"
'python'
"""python"""
'''python'''
cv
변수에 It's my velog
저장하기>>> cv = "It's my velog"
say
변수에 "It's my velog", he says.
저장하기>>> say = '"It is my velog", he says.'
백슬래시(\)
사용하기>>> cv = 'It\'s my velog'
>>> say = "\"It is my velog\", he says."
multiline
변수에...저장하기I love music.
My best thing is dusk till dawn.
\n
삽입하기>>> multiple = "I love music.\nMy best thing is dusk till dawn."
>>> multiline = """
I love music.
My best thing is dusk till dawn.
"""
>>> head = "Hi, "
>>> tail = "nice to meet you."
>>> head + tail
'Hi, nice to meet you.'
>>> a = 'python'
>>> a * 2
'pythonpython'
>>> a = "Hi, nice to meet you."
>>> len(a)
21
인덱싱(Indexing)은 무언가를 가르키는 것
슬라이싱(Slicing)은 무언가를 잘라내는 것
>>> a = "Hi, nice to meet you."
>>> a[5]
'i'
>>> a[0]
'H'
>>> a = "Hi, nice to meet you."
>>> a[-1]
'.'
>>> a[-6]
't'
>>> a = "Hi, nice to meet you."
>>> a[0:2]
'Hi'
>>> a = "Hi, nice to meet you."
>>> a[0:2]
'Hi'
>>> a[:2]
'Hi'
>>> a[4:8]
'nice'
>>> a[8:]
' to meet you.'
>>> a[:]
'Hi, nice to meet you.'
>>> a[4:-13]
'nice'
>>> a = "20210529Rainy"
>>> date = a[:8]
>>> weather = a[8:]
>>> date
'20210529'
>>> weather
'Rainy'
>>> "I have %d pens." %3
'I have 3 pens.'
>>> "I have %s pens." %"three"
'I have three pens.'
>>> number = 3
>>> "I have %d pens." %number
'I have 3 pens.'
>>> number = 5
>>> day = "two"
>>> "I have %d pens that i bought %s days ago" %(number, day)
>>> "I have %s pens." %5
'I have 5 pens.'
>>>"rate is %s" %90.3
'rate is 90.3'
>>> "Its possibility is %d%%." %90
'Its possibility is 90%.'
>>> "%10s" %"Hi"
Hi
>>> "%-10sTom" %"Hi"
Hi Tom
>>> "%0.4f"% 3.141592
'3.1416'
>>> "%10.4f"%3.141592
' 3.1416'
>>> "I have {0} pens".format(3)
'I have 3 pens
>>> "I have {0} pens".format("three")
'I have three pens'
>>> number = 3
>>> "I have {0} pens".format(number)
>>> number = 5
>>> day = "two"
>>> "I have {0} pens that i bought {1} days ago".format(number, day)
'I have 5 pens that i bought two days ago
>>> "I have {number} pens that i bought {day} days ago".format(number = 5, day = "two")
'I have 5 pens that i bought two days ago'
>>> "I have {0} pens that i bought {day} days ago".format(3, day="two")
'I have 3 pens that i bought two days ago'
>>> "{0:<10}".format("Hi")
'Hi '
>>> "{0:>10}".format("Hi")
' Hi'
>>>"{0:^10}".format("Hi")
' Hi '
>>> "{0:=^10}".format("Hi")
'====Hi===='
>>> "{0:#<10}".format("Hi")
'Hi########'
>>> a = 3.141592
>>> "{0:0.4f}".format(a)
'3.1416'
>>> "{0:10.4f}".format(a)
' 3.1416'
{
또는 }
문자 표현하기>>> "{{ Hi }}".format()
'{ Hi }'
>>> number = 3
>>> day = "two"
>>> f'I have {number} pens that i bought {day} days ago'
'I have 3 pens that i bought two days ago'
>>> f'I have {number + 1} pens that i bought {day} days ago'
'I have 4 pens that i bought two days ago'
>>> d = {'number':3, 'day':'two'}
>>> f'I have {d["number"]} pens that i bought {d["day"]} days ago'
'I have 3 pens that i bought two days ago'
>>> f'{"Hi":<10}'
'Hi '
>>> f'{"Hi":>10}'
' Hi'
>>> f'{"Hi":^10}'
' Hi '
>>> f'{"Hi":=^10}'
'====Hi===='
>>> f'{"Hi":#<10}'
'Hi########'
>>> a = 3.141592
>>> f'{a:0.4f}'
'3.1416'
>>> f'{a:10.4f}'
' 3.1416'
{
또는 }
문자 표현하기>>> f'{{ Hi }}'
'{ Hi }'
문자열 자료형은 자체적 함수를 가지고 있다. 문자열 변수 뒤에 '.'을 붙인 다음 함수 이름을 써주면 된다.
>>> a = "bubble"
>>> a.count('b')
3
>>> a = "I have 3 pens that i bought two days ago"
>>> a.find('s')
12
>>> a.find('c')
-1
>>> a = "I have 3 pens that i bought two days ago"
>>> a.index('s')
12
>>> a.index('c')
Traceback (most recent call last):
File "main.py", line 1, in <module>
print(a.index('c'))
ValueError: substring not found
이 함수는 문자열 뿐만 아니라 리스트나 튜플도 입력할 수 있다.
>>> ",".join('ABCDE')
A,B,C,D,E
>>> a = "hi"
>>> a.upper()
'HI'
>>> b = "HI"
>>> b.lower()
'hi'
>>> a = " Hi "
>>> a.lstrip()
'Hi '
>>> a.rstrip()
' Hi'
>>> a.strip()
'Hi'
>>> a = "My favorite food is pizza"
>>> a.replace('pizza','chicken')
'My favorite food is chicken'
괄호 안에 아무것도 넣어 주지 않으면 공백(스페이스, 탭, 엔터 등)을 기준으로 문자열을 나눈다.
>>> a = "My favorite food is pizza"
>>> a.split()
['My' ,'favorite' ,'food' ,'is' ,'pizza']
>>> b = "a;b;c;d"
>>> b.split(";")
['a', 'b', 'c', 'd']
>>> a = pithon
>>> a[1] = 'y'
Traceback (most recent call last):
File "main.py", line 2, in <module>
a[1] = "y"
TypeError: 'str' object does not support item assignment
코드 | 설명 |
---|---|
%s | 문자열(String) |
%c | 문자 1개(character) |
%d | 정수(Integer) |
%f | 부동소수(floating-point) |
%o | 8진수 |
%x | 16진수 |
%% | Literal %(문자 % 자체) |
find와 index 둘 다 문자열 중 가장 먼저 나온 위치를 반환한다. 하지만 찾는 문자나 문자열이 존재하지 않는다면 find는 -1을, index는 오류를 발생시킨다.