1、字符串型是一个 Python 序列类型,它的本质是字符序列。Python基本元素:数字、字符串和变量 与其他语言不同的是,Python 字符串是不可变的。你无法对原字符串进行修改,但可以将
字符串的一部分复制到新字符串,来达到相同的修改效果。
2、将一系列字符包裹在一对单引号或一对双引号中即可创建字符串,就像下面这样:

‘Snap’
‘Snap’

“Crackle”
‘Crackle’

交互式解释器输出的字符串永远是用单引号包裹的,但无论使用哪种引号,Python 对字符
串的处理方式都是一样的,没有任何区别。
3、为什么要使用两种引号?这么做的好处是可以创建本身就包含引号的字符串,
而不用使用转义符。可以在双引号包裹的字符串中使用单引号,或者在单引号包裹的字符
串中使用双引号:

“‘Nay,’ said the naysayer.”
“‘Nay,’ said the naysayer.”

‘The rare double quote in captivity: ".’
‘The rare double quote in captivity: ".’

‘A “two by four” is actually 1 1⁄2" × 3 1⁄2".’
‘A “two by four is” actually 1 1⁄2" × 3 1⁄2".’

“‘There’s the man that shot my paw!’ cried the limping hound.”
“‘There’s the man that shot my paw!’ cried the limping hound.”

4、还可以使用连续三个单引号 ‘’’,或者三个双引号 “”" 创建字符串:

‘’‘Boom!’’’
‘Boom’

“”“Eek!”""
‘Eek!’

三元引号在创建短字符串时没有什么特殊用处。它多用于创建多行字符串。下面的例子
中,创建的字符串引用了 Edward Lear 的经典诗歌:

poem = ‘’‘There was a Young Lady of Norway,
… Who casually sat in a doorway;
… When the door squeezed her flat,
… She exclaimed, “What of that?”
… This courageous Young Lady of Norway.’’’

(上面这段代码是在交互式解释器里输入的,第一行的提示符为 >>>,后面行的提示符
为 …,直到再次输入三元引号暗示赋值语句的完结,此时光标跳转到下一行并再次以 >>>
提示输入。)
如果你尝试通过单独的单双引号创建多行字符串,在你完成第一行并按下回车时,Python
会弹出错误提示:

poem = 'There was a young lady of Norway,
File “”, line 1
poem = 'There was a young lady of Norway,
^
SyntaxError: EOL while scanning string literal

在三元引号包裹的字符串中,每行的换行符以及行首或行末的空格都会被保留:

poem2 = ‘’‘I do not like thee, Doctor Fell.
… The reason why, I cannot tell.
… But this I know, and know full well:
… I do not like thee, Doctor Fell.
… ‘’’

print(poem2)
I do not like thee, Doctor Fell.
The reason why, I cannot tell.
But this I know, and know full well:
I do not like thee, Doctor Fell.

5、为什么会用到空字符串?有些时候你想要创建的字符串可能源自另一字符串的内容,这时
需要先创建一个空白的模板,也就是一个空字符串。

bottles = 99
base = ‘’

base += 'current inventory: ’
base += str(bottles)
base
‘current inventory: 99’

6、使用 str() 可以将其他 Python 数据类型转换为字符串:

str(98.6)
‘98.6’

str(1.0e4)
‘10000.0’

str(True)
‘True’

当你调用 print() 函数或者进行字符串差值(string interpolation)时,Python 内部会自动
使用 str() 将非字符串对象转换为字符串。
7、Python 允许你对某些字符进行转义操作,以此来实现一些难以单纯用字符描述的效果。在
字符的前面添加反斜线符号 \ 会使该字符的意义发生改变。最常见的转义符是 \n,它代表
换行符,便于你在一行内创建多行字符串。

palindrome = ‘A man,\nA plan,\nA canal:\nPanama.’
print(palindrome)
A man,
A plan,
A canal:
Panama.

转义符 \t(tab 制表符)常用于对齐文本,之后会经常见到:

print(’\tabc’)
abc

print(‘a\tbc’)
a bc

print(‘ab\tc’)
ab c

print(‘abc\t’)
abc

(上面例子中,最后一个字符串的末尾包含了一个制表符,当然你无法在打印的结果中看
到它。)
有时你可能还会用到 ’ 和 " 来表示单、双引号,尤其当该字符串由相同类型的引号包裹时:

testimony = ““I did nothing!” he said. “Not that either! Or the other
thing.””

print(testimony)
“I did nothing!” he said. “Not that either! Or the other thing.”

fact = “The world’s largest rubber duck was 54’2” by 65’7" by 105’"

print(fact)
The world’s largest rubber duck was 54’2" by 65’7" by 105’

如果你需要输出一个反斜线字符,连续输入两个反斜线即可:

speech = ‘Today we honor our friend, the backslash: \.’
print(speech)
Today we honor our friend, the backslash: .

8、在 Python 中,你可以使用 + 将多个字符串或字符串变量拼接起来,就像下面这样:

'Release the kraken! ’ + ‘At once!’
‘Release the kraken! At once!’

也可以直接将一个字面字符串(非字符串变量)放到另一个的后面直接实现拼接:

"My word! " “A gentleman caller!”
‘My word! A gentleman caller!’

进行字符串拼接时,Python 并不会自动为你添加空格,需要显示定义。但当我们调用
print() 进行打印时,Python 会在各个参数之间自动添加空格并在结尾添加换行符:

a = ‘Duck.’
b = a
c = ‘Grey Duck!’
a + b + c
‘Duck.Duck.Grey Duck!’

print(a, b, c)
Duck. Duck. Grey Duck!

9、使用 * 可以进行字符串复制。试着把下面这几行输入到交互式解释器里,看看结果是
什么:

start = 'Na ’ * 4 + ‘\n’
middle = 'Hey ’ * 3 + ‘\n’
end = ‘Goodbye.’
print(start + start + middle + end)

python字符串和整数的区别 python字符串与数字的区别_bc

10、在字符串名后面添加 [],并在括号里指定偏移量可以提取该位置的单个字符。第一个字符
(最左侧)的偏移量为 0,下一个是 1,以此类推。最后一个字符(最右侧)的偏移量也可
以用 -1 表示,这样就不必从头数到尾。偏移量从右到左紧接着为 -2、-3,以此类推。

letters = ‘abcdefghijklmnopqrstuvwxyz’
letters[0]
‘a’

letters[1]
‘b’

letters[-1]
‘z’

letters[-2]
‘y’

letters[25]
‘z’

letters[5]
‘f’

如果指定的偏移量超过了字符串的长度(记住,偏移量从 0 开始增加到字符串长度 -1),
会得到一个异常提醒:

letters[100]
Traceback (most recent call last):
File “”, line 1, in
IndexError: string index out of range

位置索引在其他序列类型(列表和元组)中的使用也是如此
11、由于字符串是不可变的,因此你无法直接插入字符或改变指定位置的字符。看看当我们试
图将 ‘Henny’ 改变为 ‘Penny’ 时会发生什么:

name = ‘Henny’
naume[0] = ‘P’
Traceback (most recent call last):
File “”, line 1, in
TypeError: ‘str’ object does not support item assignment

为了改变字符串,我们需要组合使用一些字符串函数,例如 replace(),以及分片操作:

name = ‘Henny’
name.replace(‘H’, ‘P’)
‘Penny’

‘P’ + name[1:]
‘Penny’