문자열


#!/usr/bin/python3
# 문자열
# by Elex (http://python.elex.pe.kr/)


def main():
   
    print( "This is a string." .upper()) # THIS IS A STRING.
    print( "This is a string." .lower()) # this is a string.
    print( "this is a string." .capitalize()) # This is a string.
    print( "This Is A String." .swapcase()) # tHIS iS a sTRING.
   
    print( "This is a string." .find("is")) # 2
    print( "This is a string." .replace("This", "That")) # That is a string.
       
    print( "  This is a string.  " .strip()) # This is a string.
    print( "  This is a string.  " .strip("\n")) #   This is a string. 
    print( "  This is a string.  " .rstrip()) #   This is a string.
    print( "  This is a string.  " .lstrip()) # This is a string. 
 
    print( "This is a string." .isalnum()) # False
    print( "This is a string." .isalpha()) # False
    print( "This is a string." .isdigit()) # False
    print( "This is a string." .isprintable()) # True
   
    print( "This is a string." .split()) # ['This', 'is', 'a', 'string.']
    print( "This is a string." .split('i')) # ['Th ', 's ', 's a str', 'ng.']
   
    print( "-".join(['This', 'is', 'a', 'string.' ])) # This-is-a-string.
   
    a, b = 3, 5
    print( "a는 {}이고, b는 {}이다." .format(a, b))
    print( "b는 {1}이고, a는 {0}이다." .format(a, b))
   
    d = dict(first = 3, second = 5 )
    print( "퍼스트는 {first}이고, 세컨드는 {second}이다." .format(**d))
   
if __name__ == "__main__": main()


문자열을 다룸에 있어서, 파이썬과 다른 프로그래밍 언어와의 가장 큰 차이점 중 하나는 파이썬 언어에서는 문자열 리터럴도 메서드를 연결해서 쓸 수 있다는 것이다.
위의 소스 코드는 몇 가지 유용한 파이썬 문자열 메서드들을 나열한 것이다.

댓글

이 블로그의 인기 게시물

Project Euler - 3

메인 스크립트

예외 처리