2/24/2014

(python study) string functions

__author__ = 'mare'

#STRING functions

#capitalize()
print( "PYTHON".capitalize() )
#Python

print( "Python is powerful".capitalize() )
#Python is powerful


#count(keyword, [start, [end]])
print( "python is powerful".count('p') )
print( "python is powerful".count('p', 5) ) #[5:]
print( "python is powerful".count('p', 0, -1) ) #[0:-1]
#2
#1
#2


#encode([encoding, [errors]])
print( "๊ฐ€๋‚˜๋‹ค".encode('cp949') )
print( "๊ฐ€๋‚˜๋‹ค".encode('utf-8') )
#b'\xb0\xa1\xb3\xaa\xb4\xd9'
#b'\xea\xb0\x80\xeb\x82\x98\xeb\x8b\xa4'

#endswith(postfix, [start, [end]])
print( "python is powerful".endswith('ful'))
print( "python is powerful".endswith('ful', 5))
print( "python is powerful".endswith('ful', 5, -1))
#True
#True
#False

print( "python is powerful".endswith(('m', 'l')) )
#True


#expandtabs([tabsize])
print( "python\tis\tpowerful".expandtabs() )
print( "python\tis\tpowerful".expandtabs(1) )
#python  is      powerful
#python is powerful

#find(keyword, [start, [end]])
print( "python is powerful".find('p') )
print( "python is powerful".find('p', 5, -1) )
print( "python is powerful".find('pa') )
#0
#10
#-1


#index(keyword, [start, [end]])
print( "python is powerful".index('p') )
print( "python is powerful".index('p', 5, -1) )
#0
#10

#print( "python is powerful".index('pa') )
#Traceback (most recent call last):
#File "/Users/mare/PycharmProjects/str_handling/str_handle.py", line 58, in 
#print( "python is powerful".index('pa') )
#ValueError: substring not found


#isalnum()
print( "python".isalnum() )
print( "python3000".isalnum() )
print( "python3.2".isalnum() )
#True
#True
#False


#isalpha()
print( "python".isalpha() )
print( "python3000".isalpha() )
#True
#False



#islower()
print( "python".islower() )
print( "Python".islower() )
print( "python3.2".islower() )
#True
#False
#True


#isspace() space, tab, change line... is ture
print( " ".isspace() )
print( "\t\n".isspace() )
print( "\thi\n".isspace() )
#True
#True
#False


#istitle()
print( "python is powerful".istitle() )
print( "PYTHON IS POWERFUL".istitle() )
print( "Python Is Powerful".istitle() )
#False
#False
#True

#isupper()
print("python".isupper())
print("PYTHON".isupper())
print("PYTHON3.2".isupper())
#False
#True
#True


#isdecimal(), isdigit()
print( "2580".isdecimal() )
#True

#isnumeric()
print( '\u00bc'.isdecimal() )
print( '\u00bc'.isnumeric() )
#False
#True

print("id_1".isidentifier())
print("1_id".isidentifier())
#True
#False


#isprintable()
print("test".isprintable() )
print( '\u0014'.isprintable() )
#True
#False


#join(sequence)
print(".".join("HOT"))
print("\t".join(["python","is","powerful"]))
#H.O.T
#python is powerful

#lstrip([chars])
print( "\t python".lstrip())
print( ">>> python is powerful".lstrip("> "))
#python
#python is powerful


#maketrans(x, [y, [z]])
transmap = str.maketrans( {"p":"P"} )
print( "python is powerful".translate(transmap) )
#Python is Powerful

transmap = str.maketrans( "poieu", "P0129" )
print( "python is powerful".translate(transmap) )
#Pyth0n 1s P0w2rf9l

transmap = str.maketrans( "p", "P", "!" )
print( "python is powerful!!!".translate(transmap) )
#Python is Powerful


#partition(separator)
print( "python is powerful".partition("is") )
#('python ', 'is', ' powerful')

#replace(old, new, [count])
print( "python is powerful".replace("p", "P"))
print( "python is powerful".replace("p", "P", 1))
#Python is Powerful
#Python is powerful


#rfind(keyword, [start, [end]])
print( "python is powerful".rfind('p'))
print( "python is powerful".rfind('p', 0, 9))
print( "python is powerful".rfind('pa'))
#10
#0
#-1

#rindex(keyword, [start, [end]])
print( "python is powerful".rindex('p'))
#10

#rpartition(separator)
print( "python is powerful".rsplit())
print( "python is powerful".rsplit(' ',1))
#['python', 'is', 'powerful']
#['python is', 'powerful']


#rsplit([separator, [maxsplit]])
print( "python is powerful".rsplit() )
print( "python is powerful".rsplit(' ',1) )
#['python', 'is', 'powerful']
#['python is', 'powerful']


#rstrip([chars])
print( "python \t".rstrip() )
#python


#split([separator, [maxsplit]])
print("python is powerful".split() )
print("python is powerful".split(' ', 1))
#['python', 'is', 'powerful']
#['python', 'is powerful']


#splitlines([keep])
print( "python\r\nis\npowerful".splitlines() )
#['python', 'is', 'powerful']


#startswith(prefix, [start, [end]])
print( "python is powerful".startswith('py'))
print( "python is powerful".startswith('py', 5))
print( "python is powerful".startswith('py', 0, 5))
print( "python is powerful".startswith(('p', 'm')))
#False
#True
#True


#strip([chars])
print( "\t python \t".strip() )
print( ">>> python <<<".strip("<> ") )
#python
#python

#swapcase()
print("Python3.2".swapcase() )
#pYTHON3.2

#title()
print( "python is powerful".title() )
#Python Is Powerful

#upper()
print(" Python3.2".upper() )
#PYTHON3.2


No comments:

Post a Comment