string 모듈

2015.03.20 20:50

출처 : http://effbot.org/librarybook/string.htm


[*] Using the string Module

import string text = "Monty Python's Flying Circus" print "upper", "=>", string.upper(text) print "lower", "=>", string.lower(text) print "split", "=>", string.split(text) print "join", "=>", string.join(string.split(text), "+") print "replace", "=>", string.replace(text, "Python", "Java") print "find", "=>", string.find(text, "Python"), string.find(text, "Java") print "count", "=>", string.count(text, "n")

upper => MONTY PYTHON'S FLYING CIRCUS lower => monty python's flying circus split => ['Monty', "Python's", 'Flying', 'Circus'] join => Monty+Python's+Flying+Circus replace => Monty Java's Flying Circus find => 6 -1 count => 3



[*] Using string methods instead of string module functions

text = "Monty Python's Flying Circus" print "upper", "=>", text.upper() print "lower", "=>", text.lower() print "split", "=>", text.split() print "join", "=>", "+".join(text.split()) print "replace", "=>", text.replace("Python", "Perl") print "find", "=>", text.find("Python"), text.find("Perl") print "count", "=>", text.count("n")

upper => MONTY PYTHON'S FLYING CIRCUS lower => monty python's flying circus split => ['Monty', "Python's", 'Flying', 'Circus'] join => Monty+Python's+Flying+Circus replace => Monty Perl's Flying Circus find => 6 -1 count => 3



[*] Using the string module to convert strings to numbers

   import string

print int("4711"), print string.atoi("4711"), print string.atoi("11147", 8), # octal print string.atoi("1267", 16), # hexadecimal print string.atoi("3mv", 36) # whatever... print string.atoi("4711", 0), print string.atoi("04711", 0), print string.atoi("0x4711", 0) print float("4711"), print string.atof("1"), print string.atof("1.23e5")

4711 4711 4711 4711 4711 4711 2505 18193 4711.0 1.0 123000.0



[*] ETC

source:

import string


print string.letters

print string.punctuation

print string.digits


result :

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz

!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~

0123456789



'List > Python' 카테고리의 다른 글

Quick Sort  (0) 2015.06.18
import random  (0) 2015.06.17
Mail 보내기  (0) 2015.04.30
pattern생성 및 offset 확인  (0) 2015.03.31
string 모듈  (0) 2015.03.20
정규표현식 정리  (0) 2015.03.19

smleenull List/Python

티스토리 툴바