Python中 str类方法(1)

capitalize()  字符串首字母大写

1 str1="ni hao ma"
2 str2=str1.capitalize()
3 print(str2)
#输出: Ni hao ma

 

center(width, fillchar=None)  将字符串放在中间;在制定长度下,首尾以指定字符填充

str1="this"
str2=str1.center(20,"*")
print(str2)

#输出:********this********

str1="this"
str2=str1.center(5,"*")
print(str2)
#输出:*this

str1="this"
str2=str1.center(6,"*")
print(str2)
#输出:*this*

 

count(sub, start=None, end=None)  计算某字符在字符串中的数量

str1="this is string"
num=str1.count("i")
print(num)

#输出:3

 

decode(encoding=None, errors=None):解码

encode(self, encoding=None, errors=None):编码

 

endswith(self, suffix, start=None, end=None)  判断是否以某字符结尾

str1="this is string"
num=str1.endswith("ing")
print(num)

#输出:True

 

expandtabs(self, tabsize=None)   返回制表符,tabsize此选项指定要替换 为制表符符"/h"的字符数    默认为8

str1='this\tis\ta\tstring.'
str2=str1.expandtabs()
print(str2)

#输出:this    is      a       string.

 

find(self, sub, start=None, end=None)  在字符串中寻找指定字符的位置

str1='this is a string.'
#找到的时候
num=str1.find("h")
print(num)

#输出:1

#找不到的时候 返回-1
num=str1.find("x")
print(num)
#输出:-1

 

format(*args, **kwargs)  类似%s的用法,它通过{}来实现

str1='my name is {0},Age {1}'
num=str1.format("wang","26")
print(num)

#输出:my name is wang,Age 26

 

index(self, sub, start=None, end=None)  类似find  但是如果找不到的情况下,程序会报错

 

 

isalnum(self)  判断字符串中是否都是数字和字母,如果是返回True,相反返回false  ;注意空格 符号 都会返回false ; 只有数字 或者只有字母 返回True

str1='my name is {0},Age {1}'
num=str1.isalnum()
print(num)

#输出:false

str1='1aareawr23'
num=str1.isalnum()
print(num)

#输出:True

 

 

isalpha(self)  判断字符串中是否都是字母,如果是则返回True,否则返回False

isdigit(self)  判断字符串中是否都是数字,如果是则返回True,否则返回False

 

islower(self)  判断字符串中的字母是否都是小写,如果是返回True,否则返回False

 1 str1='wasd fdfdsa'
 2 num=str1.islower()
 3 print(num)
 4 
 5 #输出:True
 6 
 7 str1='lAO wang'
 8 num=str1.islower()
 9 print(num)
10 
11 #输出:Flase

 

isspace(self)  判断字符串是否都是空格;如果是返回True;否则返回false

string = ' '
new_str = string.isspace()
print(new_str)
#输出:True
string = 'My name is Yue,my age is 18.'
new_str = string.isspace()
print(new_str)
#输出:False

 

 istitle(self)  判断字符串中所有的单词首字母是否都是大写, 如果是返回True ,否则返回false

string = 'My Name Is Yue.'
new_str = string.istitle()
print(new_str)
#输出:True
string = 'My name is Yue,my age is 18.'
new_str = string.istitle()
print(new_str)
#输出:False

 

 isupper(self)  判断字符串中所有字母是否是大写   是返回True   不是返回false

string = 'MY NAME IS YUE.'
new_str = string.isupper()
print(new_str)
#输出:True
string = 'My name is Yue.'
new_str = string.isupper()
print(new_str)
#输出:False

 

 join(self,iterable)  将序列中的元素以指定的字符连接生成一个新的字符串

string = ("haha","lala","ohoh")
str = "-"
print(str.join(string))
#输出:haha-lala-ohoh

 

lower(self)  转换字符串中所有大写字符为小写。

string = "My Name is YUE."
print(string.lower())
# 输出:my name is yue.

 

ljust(self, width, fillchar=None)  返回一个原字符串左对齐,并使用空格填充至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串。

string = "My name is wang."
print(string.ljust(18))
#输出:My name is wang.                          

 

lstrip(self, chars=None)  截掉字符串左边的空格或指定字符

string = " My Name is YUE."
print(string.lstrip())
#输出:My Name is YUE.
string = "My Name is YUE."
print(string.lstrip('My'))
#输出: Name is YUE.

 

partition(self, sep)    根据指定的分隔符将字符串进行分割

string = "http://www.mingyuanyun.com"
print(string.partition('://'))
#输出:('http', '://', 'www.mingyuanyun.com')

 

 replace(self, old, new, count=None)  把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。

string = "My name is yue."
print(string.replace("yue","ying"))
#输出:My name is ying.

 

split(self, sep=None, maxsplit=None)  通过指定分隔符对字符串进行切片。

string = "haha lala gege"
print(string.split(' '))
#输出:['haha', 'lala', 'gege']
print(string.split(' ', 1 ))
#输出: ['haha', 'lala gege']

 

swapcase(self)  对字符串的大小写字母进行转换。

string = "My Name Is Yue."
print(string.swapcase())
#输出:mY nAME iS yUE.

 

 title(self)  返回"标题化"的字符串,就是说所有单词都是以大写开始,其余字母均为小写(见 istitle())。

string = "my name is yue,my age is 18."
print(string.title())
#输出:My Name Is Yue,My Age Is 18.

 

upper(self)  将字符串中的小写字母转为大写字母。

string = "my name is yue,my age is 18."
print(string.upper())
#输出:MY NAME IS YUE,MY AGE IS 18.

 

zfill(self, width)  返回指定长度的字符串,原字符串右对齐,前面填充0。

string = "my name is yue."
print(string.zfill(18))
#输出:000my name is yue.

translate(self, table, deletechars=None)  根据参数table给出的表(包含 256 个字符)转换字符串的字符, 要过滤掉的字符放到 del 参数中。

from string import maketrans
str = "aoeiu"
num = "12345"
trantab = maketrans(str, num)
string = "my name is yue"
print(string.translate(trantab))
# 输出:my n1m3 4s y53

 

strip(self, chars=None): 去掉字符串前后的空格和换行符

 

1 s="   wang,binbin\n "
2 s=s.strip()
3 print(s)
4 #输出:wang,binbin

 

posted @ 2017-06-20 20:14  斌哥骑猿看唱本  阅读(1883)  评论(0编辑  收藏  举报