Python string

import string

mystring= 'its master will touch its tail.'
myindex = 0
print mystring[3:7]+';'
print len(mystring)




print mystring.lower()
print mystring.upper()
print mystring.swapcase()
print mystring.capitalize()
print string.capwords(mystring)
print mystring.title()	# upper the first char in each word




width = 40
fillchar = '#'

print mystring.ljust(width,fillchar)
print mystring.rjust(width,fillchar)
print mystring.center(width,fillchar)
print mystring.zfill(width)






start = 0
end = 32
substr = 'its'

# mystring.find(substr,[start,[end]])
print mystring.find(substr,start,end)
# search forward,return the index of the first char of the first substr,searched in mystring[start:end]

# mystring.index(substr, [start, [end]])
print mystring.index(substr,start,end)
# similar to s.find(), if there is no such substr, return an error

# mystring.rfind(substr, [start, [end]])
print mystring.rfind(substr,start,end)
# search backward,return the index of the first char of the first substr,searched in mystring[start:end]

# mystring.rindex(substr, [start, [end]]))
print mystring.rindex(substr,start,end)
# similar to s.rfind(), if there is no such substr, return an error

# mystring.count(substr, [start, [end]]))
print mystring.count(substr,start,end)
# caculate the times substr appears in mystring[start:end]mystring[start:end]





oldstr = 'its'
newstr = 'his'
count = 1
# mystring.replace(oldstr, newstr, [count])
print mystring.replace(substr,newstr,count)
# replace the oldstr with newstr, 'count' times

mystring = '    hello\t\n'
# mystring.strip([chars])
# if chars == null, delete '\n','\r','\t',' '
# else, delete each char in chars
print mystring.strip()
print mystring.lstrip()

mystring = '212341212hello12'
print mystring.strip('1234')

print mystring.lstrip()

tabsize = 2
print mystring.replace(' ','\t').expandtabs(1)





mystring = 'what|can|i|do|for|you.'

sep = '|'
maxsplit = 3
# mystring.split([sep,[maxsplit]])
mylist = mystring.split(sep,maxsplit)
print mylist

mylist = mystring.rsplit(sep,maxsplit)
print mylist




mystring = 'hello\ncan you help me\nyes.'

# mystring.splitlines([keepends])
print mystring.splitlines(0)
print mystring.splitlines(1)




mysplit = '|'
seq = 'abcd ef'
# mystring.join(seq)
print mysplit.join(seq)





myfrom = 'abcde'
myto   = '12345'
table = string.maketrans(myfrom,myto)

mystring = 'aabbabcde'
# mystring.translate(table[,deletechars])
print mystring.translate(table,'d')





# about codecs modules
# encode([encoding,[errors]])
# 'encoding' can be 'gb2312','gbk','gb18030','bz2','zlib','big5','bzse64',etc
# 'errors' default be 'strict', meaning 'UnicodeError', it can be 'ignore','replace','xmlcharrefreplace','backslashreplace' as a option
mystring = 'what can i do for you'
print mystring.encode('gbk')

# decode([decoding,[errors]])
print mystring.decode('utf-8')





mystring = 'hello, this is Mr.Ding'
print mystring.startswith('hello')
print mystring.endswith('ding')




mystring = 'hehe111'

print mystring.isalnum()
# if the string consists of only chars and numbers
print mystring.isalpha()
print mystring.isdigit()
print mystring.isspace()
# if, only space
print mystring.islower()
# if, without upper char
print mystring.isupper()
mystring = 'Hello And You'
print mystring.istitle()





mystring = '1234'
# string.atoi(s[,base])
# base default 10
print string.atoi(mystring,10)+1
print string.atol(mystring,10)+1

mystring = '123.01111'
print string.atof(mystring)+0.01






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值