>>> a = 3
>>> b = 4
>>> a ** b
81
>>> 7 // 4
1
줄을 바꾸는 이스케이프 코드 '\n' 사용을 하지 않기 위해
mutiline = '''
Life is too short
You need python
'''
끝번호는 포함하지 않는다. ex) 0 <= a < 4
끝번호 생략시 문자열 끝까지 출력
>>> a = "Life is too short, You need Python"
>>> a[0:4]
'Life'
>>> a[19:]
'You need Python'
>>> "I eat %d apples." % 3
'I eat 3 apples.'
>>> number = 10
>>> day = "three"
>>> "I ate %d apples. so I was sick for %s days." % (number, day)
'I ate 10apples. so I was sick for three days.'
>>> "Error is %d%%." % 98
'Error is 98%'
좀 더 발전된 스타일
>>> "I eat {0} apples".format(3)
'I eat 3 apples'
>>> number = 10
>>> day = "three"
>>> "I ate {0} apples. so I was sick for {1} days.".format(number, day)
'I ate 10 apples. so I was sick for three days.'
>>> "I ate {number} apples. so I was sick for {day} days.".format(number=10, day=3)
'I ate 10 apples. so I was sick for 3 days.'
>>> d = {'name':'홍길동', 'age':30}
>>> f'나의 이름은 {d["name"]}입니다. 나이는 {d["age"]}입니다.'
'나의 이름음 홍길동입니다. 나이는 30입니다.'
>>> a = "hobby"
>>> a.cout('b')
2
>>> a = "Python is the best choice"
>>> a.find('b')
14
>>> a.find('k')
-1
>>> a = "Life is too short"
>>> a.index('t')
8
>>> a.index('k')
Traceback (most recent call last):
File "<stdin>", line 1, in >module>
ValueError: substring not found
>>> ",".join('abcd')
'a,b,c,d'
>>> a = "hi"
>>> a.upper()
'HI'
>>> a = "HI"
>>> a.lower()
'hi'
>>> a = " hi "
>>> a.lstrip()
'hi '
>>> a = " hi "
>>> a.rstrip()
' hi'
>>> a = " hi "
>>> a.strip()
'hi'
>>> a = "Life is too short"
>>> a.replace("Life", "Your leg")
'Your leg is too short'
인자값이 없는 경우 공백을 기준으로 나눈다.
>>> a = "Life is too short"
>>> a.split()
['Life', 'ls', 'too', 'short']
>>> b = "a:b:c:d"
>>> b.split(':')
['a','b','c','d']