파이썬 문법 - Strings

Junyeong Choi·2021년 5월 13일

x = 'Hello World
x.upper() = 'Hello World -> 'HELLO WORLD'
x.lpper() = 'Hello World -> 'hello world'
x.split() = 'Hello World -> 'Hello', 'World'
x.split('l') = 'Hello World -> 'He', 'l', 'l', 'o Wor', 'l', 'd'

파이썬 format()

print('This is a string {}'.format('inserted')) -> 'This is a string inserted'

print('The {2} {1} {0}'.format('fox', 'brown', 'quick' )) -> 'The quick brown fox'

print('The {q} {b} {f}'.format(f='fox', b='brown', q='quick' )) -> 'The quick brown fox'

print('The {q} {b} {f}'.format(f='fox', b='brown', q='quick' )) -> 'The quick brown fox'


소수점

Float formatting은 "{value:width.precision f}"를 따른다.
result = 100/777 -> 0.1287001287001287
print("The result is {r:1.5f}".format(r=result)) -> The result is 0.12870

만약 result = 104.12345 그렇다면
print("The result is {r:1.5f}".format(r=result)) -> The result is 104.12345


문자열 넣기

name = Junyeong
age = 28
print(f'Hello, my name is {name}')
print(f'{name} is {age} years old')

0개의 댓글