>>> print "I eat %s apples." % "five"
I eat five apples.
숫자 변수로 대입
>>> number = 3
>>> print "I eat %d apples." % number
I eat 3 apples.
두 개 이상의 값을 치환
>>> number = 10
>>> day = "three"
>>> print "I eat %d apples. so I was sick for %s days." % (number, day)
I eat 10 apples. so I was sick for three days.
%출력할때 %를 두개를 사용해야한다.
>>> print "Error is %d%%." % 98
Error is 98%.
소숫점 표현
>>> print "%0.4f" % 3.42134234
3.4213
소숫점 표현에서 왼쪽으로 10칸 공백 붙인다.
>>> print "%10.4f" % 3.42134234
3.4213
문자열 수정 도구들
양쪽 공백 지우기 (strip)
>>> a = " hi "
>>> a.strip()
'hi'
문자열 치환하기
>>> a = "Life is too short"
>>> a.replace("Life", "Your leg")
'Your leg is too short'
문자열 나누기 (split)
>>> a = "Life is too short"
>>> a.split()
['Life', 'is', 'too', 'short']
>>> a = "a:b:c:d"
>>> a.split(':')
['a', 'b', 'c', 'd']
0개의 댓글
등록된 댓글이 없습니다.