除了数值,Python可以操作字符串,它可以表现在以下几个方面。包含在单引号或双引号:
>>> 'spam eggs'
'spam eggs'
>>> 'doesn\'t'
"doesn't"
>>> "doesn't"
"doesn't"
>>> '"Yes," he said.'
'"Yes," he said.'
>>> "\"Yes,\" he said."
'"Yes," he said.'
>>> '"Isn\'t," she said.'
'"Isn\'t," she said.'
'spam eggs'
>>> 'doesn\'t'
"doesn't"
>>> "doesn't"
"doesn't"
>>> '"Yes," he said.'
'"Yes," he said.'
>>> "\"Yes,\" he said."
'"Yes," he said.'
>>> '"Isn\'t," she said.'
'"Isn\'t," she said.'
字符串可以写多行。可以用\n表示,下一行是一个合乎逻辑的延续行,最后一个字符用反斜杠:
hello = "This is a rather long string containing\n\
several lines of text just as you would do in C.\n\
Note that whitespace at the beginning of the line is\
significant."
print hello
several lines of text just as you would do in C.\n\
Note that whitespace at the beginning of the line is\
significant."
print hello
字符串可以被包围在一对三重引号里面:
print """
Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname Hostname to connect to
"""
Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname Hostname to connect to
"""
字符串可以被连接在一起,用“+”运算符,重复*:
>>> word = 'Help' + 'A'
>>> word
'HelpA'
>>> '<' + word*5 + '>'
'<HelpAHelpAHelpAHelpAHelpA>'
>>> word
'HelpA'
>>> '<' + word*5 + '>'
'<HelpAHelpAHelpAHelpAHelpA>'
两个彼此相邻的字符串文字自动连接:
>>> 'str' 'ing' # <- This is ok
'string'
>>> 'str'.strip() + 'ing' # <- This is ok
'string'
>>> 'str'.strip() 'ing' # <- This is invalid
File "<stdin>", line 1, in ?
'str'.strip() 'ing'
^
SyntaxError: invalid syntax
'string'
>>> 'str'.strip() + 'ing' # <- This is ok
'string'
>>> 'str'.strip() 'ing' # <- This is invalid
File "<stdin>", line 1, in ?
'str'.strip() 'ing'
^
SyntaxError: invalid syntax
注意:word字符串的内容是: “HelpA” 可以是下标(索引)和C一样,字符串的第一个字符下标(索引)0。可以指定的子串切片标志来表示:两个指数由冒号分隔。
>>> word[4]
'A'
>>> word[0:2]
'He'
>>> word[2:4]
'lp'
'A'
>>> word[0:2]
'He'
>>> word[2:4]
'lp'
切片索引可以使用默认值;前一个索引默认为零,第二个索引默认被切片的字符串的大小。
>>> word[:2] # The first two characters
'He'
>>> word[2:] # Everything except the first two characters
'lpA'
'He'
>>> word[2:] # Everything except the first two characters
'lpA'
和C字符串不同,Python字符串不能改变。想修改指定索引位置的字符串会导致错误:
>>> word[0] = 'x'
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: object doesn't support item assignment
>>> word[:1] = 'Splat'
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: object doesn't support slice assignment
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: object doesn't support item assignment
>>> word[:1] = 'Splat'
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: object doesn't support slice assignment
然而,创建一个新的字符串是简单而有效的:
>>> 'x' + word[1:]
'xelpA'
>>> 'Splat' + word[4]
'SplatA'
'xelpA'
>>> 'Splat' + word[4]
'SplatA'
这里是一个有用的切片操作:[:]+[:]等于。
>>> word[:2] + word[2:]
'HelpA'
>>> word[:3] + word[3:]
'HelpA'
'HelpA'
>>> word[:3] + word[3:]
'HelpA'
指数可以是负数,从右边开始计数。例如:
>>> word[-1] # The last character
'A'
>>> word[-2] # The last-but-one character
'p'
>>> word[-2:] # The last two characters
'pA'
>>> word[:-2] # Everything except the last two characters
'Hel'
'A'
>>> word[-2] # The last-but-one character
'p'
>>> word[-2:] # The last two characters
'pA'
>>> word[:-2] # Everything except the last two characters
'Hel'
相关推荐
主要介绍了C语言字符串转换为Python字符串的方法,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下
python实现字符串模糊匹配
python字符串学习笔记.python字符串操作方法
Python字符串操作常用函数,包含了检索、统计、分割、替换、大小写转换、对齐,空格删除、字符串判断(头尾+组成)
python字符串操作.pdf
python字符串学习笔记
大熊课堂,关于 python 字符串的课件
python字符串操作实方法大合集,包括了几乎所有常用的python字符串操作,如字符串的替换、删除、截取、复制、连接、比较、查找、分割等,需要的朋友可以参考下。
python字符串处理实例.docx
本文实例为大家分享了python实现字符串加密成纯数字的具体代码,供大家参考,具体内容如下 说明: 该加密算法仅仅是做一个简单的加密,安全性就不谈了,哈哈. 算法流程:
Python 字符串深度总结Python 字符串深度总结
实例如下: # bytes object b = bexample # str object s = example ...以上这篇python字符串str和字节数组相互转化方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多
Python字符串调用方法解释(部分),因为本人使用的是3.1版,很多旧版的调用方法新版不支持,所以只有些基础的调用和代码运行的截图,比较适合和我一样的菜鸟级的童鞋观摩。
python第三章,字符串的简单处理及应用。以及数据类型
1、本文主要讲解了关于字符串str的知识点,包含创建字符串、访问字符串、字符串基础知识、以及字符串的内置函数和内置方法。 2、本文件是markdown文件,是本人辛辛苦苦整理出来的,一份耕耘一分收获。 3、不要盗用,...
python字符串基本操作 Python字符串基本操作 Python是一种高级编程语言,它支持多种数据类型,其中字符串是最常用的数据类型之一。字符串是由一系列字符组成的,可以包含字母、数字、符号等。在Python中,字符串是不...
Python中使用多种方式对某一字符串的字符进行统计,实现字符串冗余字符统计,输出执行时间和内容字典
输入一个字符串,分别统计出其中英文字母、空格、数字和其它字符的个数,本文给出解决方法 编写思路: 1、字符串的遍历,和列表类似,可以把字符串当做元素都是一个字符的一个字符列表,它可以和列表有公共的语法 2...
python字符串, 列表, 字典, 集合方法说明
python字符串的使用方法 Python字符串的使用方法 Python是一种高级编程语言,它支持多种数据类型,其中字符串是最常用的数据类型之一。字符串是由一系列字符组成的,可以包含字母、数字、符号等。在Python中,字符串...