您的位置:首页 > 编程语言 > Python开发

【代码学习】PYTHON字符串的常见操作

2017-06-09 21:28 573 查看
一、字符串运算符

下表实例变量 a 值为字符串 "Hello",b 变量值为 "Python":

操作符描述实例
+字符串连接>>>a + b 'HelloPython'
*重复输出字符串>>>a * 2 'HelloHello'
[]通过索引获取字符串中字符>>>a[1] 'e'
[ : ]截取字符串中的一部分>>>a[1:4] 'ell'
in成员运算符 - 如果字符串中包含给定的字符返回 True>>>"H" in a True
not in成员运算符 - 如果字符串中不包含给定的字符返回 True>>>"M" not in a True
r/R原始字符串 - 原始字符串:所有的字符串都是直接按照字面的意思来使用,没有转义特殊或不能打印的字符。 原始字符串除在字符串的第一个引号前加上字母"r"(可以大小写)以外,与普通字符串有着几乎完全相同的语法。>>>print r'\n' \n >>> print R'\n' \n
代码:

1 a = "Hello"
2 b = "Python"
3
4 print "a + b 输出结果:", a + b
5 print "a * 2 输出结果:", a * 2
6 print "a[1] 输出结果:", a[1]
7 print "a[1:4] 输出结果:", a[1:4]
8
9 if( "H" in a) :
10     print "H 在变量 a 中"
11 else :
12     print "H 不在变量 a 中"
13
14 if( "M" not in a) :
15     print "M 不在变量 a 中"
16 else :
17     print "M 在变量 a 中"
18
19 print r'\n'
20 print R'\n'


程序执行结果为:

1 >>>
2 a + b 输出结果: HelloPython
3 a * 2 输出结果: HelloHello
4 a[1] 输出结果: e
5 a[1:4] 输出结果: ell
6 H 在变量 a 中
7 M 不在变量 a 中
8 \n
9 \n
10 >>>


二、Python 字符串格式化

Python 支持格式化字符串的输出 。尽管这样可能会用到非常复杂的表达式,但最基本的用法是将一个值插入到一个有字符串格式符 %s 的字符串中。

代码:

print "My name is %s and weight is %d kg!" % ('Zara', 21)


运行结果

My name is Zara and weight is 21 kg!

三、Python 字符串內建函数

如有字符串 mystr = "Hello World SQYY"

1、find

检测 str 是否包含在 mystr中,如果是返回开始的索引值,否则返回-1

mystr.find(str, start=0, end=len(mystr))


2、index

跟find()方法一样,只不过如果str不在 mystr中会报一个异常.

mystr.index(str, start=0, end=len(mystr))


3、count

返回 str在start和end之间 在 mystr里面出现的次数

mystr.count(str, start=0, end=len(mystr))


4、replace

把 mystr 中的 str1 替换成 str2,如果 count 指定,则替换不超过 count 次.

mystr.replace(str1, str2,  mystr.count(str1))


5、split

切割,以 str 为分隔符切片 mystr,如果 maxsplit有指定值,则仅分隔 maxsplit 个子字符串

mystr.split(str=" ", 2)


6、capitalize

把字符串的第一个字符大写

mystr.capitalize()


7、startswith

检查字符串是否是以 obj 开头, 是则返回 True,否则返回 False

mystr.startswith(obj)


8、endswith

检查字符串是否以obj结束,如果是返回True,否则返回 False.

mystr.endswith(obj)


9、lower

转换 mystr 中所有大写字符为小写

mystr.lower()


10、upper

转换 mystr 中的小写字母为大写

mystr.upper()


11、ljust

返回一个原字符串左对齐,并使用空格填充至长度 width 的新字符串

mystr.ljust(width)


12、rjust

返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串

mystr.rjust(width)


13、center

返回一个原字符串居中,并使用空格填充至长度 width 的新字符串

mystr.center(width)


14、lstrip

删除 mystr 左边的空格

mystr.lstrip()


15、rstrip

删除 mystr 字符串末尾的空格

mystr.rstrip()


16、rfind

类似于 find()函数,不过是从右边开始查找.

mystr.rfind(str, start=0,end=len(mystr) )


17、rindex

类似于 index(),不过是从右边开始.

mystr.rindex( str, start=0,end=len(mystr))


18、partition

把mystr以str分割成三部分,str前,str和str后

mystr.partition(str)


19、rpartition

类似于 partition()函数,不过是从右边开始.

mystr.rpartition(str)


20、splitlines

按照行分隔,返回一个包含各行作为元素的列表

mystr.splitlines()


21、isalnum

如果 mystr 所有字符都是字母或数字则返回 True,否则返回 False

mystr.isalnum()


22、isalpha

如果 mystr 所有字符都是字母 则返回 True,否则返回 False

mystr.isalpha()


23、isdigit

如果 mystr 只包含数字则返回 True 否则返回 False.

mystr.isdigit()


24、isspace

如果 mystr 中只包含空格,则返回 True,否则返回 False.

mystr.isspace()


25、isupper

如果 mystr 所有字符都是大写,则返回 True,否则返回 False

mystr.isupper()


26、join

mystr 中每个字符后面插入str,构造出一个新的字符串

1 mystr.join(str)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: