12.字符串的基本概念与用法

目录

一、字符串的表达方法

二、字符串输出与格式化

三、访问字符串中的值


字符串是一种表示文本的数据类型,字符串可以是ASCII字符、各种符号以及各种Unicode 字符。

本文以Python官网关于Python3.9.1中字符串的说明文档为基础,加以实例总结了字符串的基本概念、用法。

一、字符串的表达方法

字符串可以用单引号('...')或双引号("...")括起来,结果相同。[1]( \可用于转义引号,Python支持的其他转义字符见附表1)

>>> 'spam eggs'  # 单引号
'spam eggs'
>>> 'doesn\'t'  # 使用\‘转义单引号,即’按原样输出
"doesn't"  # 括起来的一对单引号自动变为双引号
>>> "doesn't"  # 使用双引号可以起到同样效果
"doesn't"
>>> '"Yes," they said.'
'"Yes," they said.'  
>>> "\"Yes,\" they said." # 使用\"转义双引号,即"按原样输出,共有两处
'"Yes," they said.'
>>> '"Isn\'t," they said.'
'"Isn\'t," they said.'

在交互式解释器中,输出字符串用引号引起来,特殊字符用反斜杠转义。尽管有时这看起来可能与输入有所不同(括起来的引号可能会更改),但这两个字符串是等效的。如果字符串包含单引号而不包含双引号,则将字符串括在双引号中;否则,将其括在单引号中。

>>> '"Isn\'t," they said.'
'"Isn\'t," they said.'
>>> print('"Isn\'t," they said.')
"Isn't," they said.
>>> s = 'First line.\nSecond line.'  # \n 意思是从下一行开始输出后面的内容
>>> s  # 未使用print(), \n作为了输出内容的一部分
'First line.\nSecond line.'
>>> print(s)  # 使用print(), \n制造了一个新行
First line.
Second line.

如果您不希望将开头的字符\解释为特殊字符,则可以通过在第一引号之前添加一个原始字符串r。

>>> print('C:\some\name')  # 这里的\n意思是创建新的一行
C:\some
ame
>>> print(r'C:\some\name')  # 注意r在引号之前
C:\some\name

字符串文字可以跨越多行。一种方法是使用三引号: """..."""'''...'''。行尾会自动包含在字符串中,但是可以通过在行尾添加 \ 来防止这种情况。下面的例子:

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

字符串可以与+运算符连接(粘合在一起),重复可使用*,*号前的数字为重复的次数

>>> # 3 times 'un', followed by 'ium'
>>> 3 * 'un' + 'ium'
'unununium'

彼此相邻的两个或多个字符串文字(即用引号引起来的文字)会自动连接在一起。

>>> 'Py' 'thon'
'Python'

当你将长字符串分行录入,相邻字符串自动连接功能将确保输出的字符串依然是完整的。 

>>> text = ('Put several strings within parentheses '
...         'to have them joined together.')
>>> text
'Put several strings within parentheses to have them joined together.'

但是,这仅适用于两个文字,不适用于变量或表达式

>>> prefix = 'Py'
>>> prefix 'thon'  # 无法将字符串和变量连接
  File "<stdin>", line 1
    prefix 'thon'
                ^
SyntaxError: invalid syntax

我们可以使用 + 号实现变量和字符串的连接

>>> prefix = 'Py'
>>> prefix + 'thon'
'Python'
附表1 Python中支持的转义字符(部分)
转义字符说明实例
\反斜杠,在行尾时的作用是续行 
\\

两个反斜杠,将第二个反斜杆以字符输出,常用于路径

path = "C:\\program\\test"

print(path)

输出:c:\program\test

\'单引号,将单引号以字符输出 
\"双引号,将双引号以字符输出 
\n换行符 
\t横向制表符 
\v纵向制表符 
\b

退格,作用是删除前一位的字符

words = "hello word \bhello"

print(words)

输出:hello wordhello #输出时删除了一个空格

\r

回车符,作用是将光标移动到该行的开始位置输出,这里做了两个实验,大家可以感受下\r的作用。

实验一:只使用\r

words = "hello world \ri love python"

print(words)

输出:

i love python

 

实验二:\n和\r结合起来使用

words = "hello world \n\ri love python"

print(words)

输出:

hello world

i love python

二、字符串输出与格式化

直接print字符串

print("hello world")

将字符串赋值给变量,然后print变量名

name = "小明"
print(name)

字符串格式化输出

print("%s is %d years old"%("Tom",17))

输出:
Tom is 17 years old

%s和%d是Python中的字符串格式化符号,%s的作用是通过str()字符串转换来格式化,%d是以十进制整数输出,详见附表2。

上面的例子,我们还可以将值存到变量中,再使用字符串格式化输出。

name = "Tom"
old = 17
print("%s is %d years old"%(name,old))

输出:
Tom is 17 years old
附表2 常见的格式化符号
格式符号描述实例
%s通过str()对字符串格式化“%s”%30 == '30'
%r通过repr()对字符串格式化

%s与%r用法基本相同,区别是%s转化为适于人阅读,%r转化为供解释器读取

'%r'%"Tom" == "'Tom'" 

print("%r %s %r %s" % ("one", "two", "three", "four"))

输出:'one' two 'three' four

%i有符号十进制整数,与%d相同"%i %i %d %d" % (11, 0b011, 0o011,  0x011) == '11 3 9 17'
%d有符号十进制整数"%i %i %d %d" % (11, 0b011, 0o011,  0x011) == '11 3 9 17'
%o八进制整数"%o %x %o %x"%(11,11,0b11,0b11) == '13 b 3 3'
%x十六进制整数"%o %x %o %x"%(11,11,0b11,0b11) == '13 b 3 3'
%e以e为底的指数标记

"%e %.2e"%(10000,11000000000) == '1.000000e+04 1.10e+10' 

小数点默认保留6位,%.2e设置小数点保留2位

%f浮点实数

"%f %.2f"%(3.1415926,3.1415926) == '3.141593 3.14'

小数点默认保留6位,%.2f设置小数点保留2位

%g

如果指数小于-4或不小于精度,则使用指数格式,否则使用十进制格式。

"%g %f %e"%(3140.123456789,3140.123456789,31400.123456789)

输出:'3140.12 3140.123457 3.140012e+04'

格式化符号实例[2]

1 x = "There are %d types of people." % 10
2 binary = "binary"
3 do_not = "don't"
4 y = "Those who know %s and those who %s." % (binary, do_not)
5
6 print x
7 print y
8
9 print "I said: %r." % x
10 print "I also said: '%s'." % y
11
12 hilarious = False
13 joke_evaluation = "Isn't that joke so funny?! %r"
14
15 print joke_evaluation % hilarious
16
17 w = "This is the left side of..."
18 e = "a string with a right side."
19
20 print w + e

输出结果为:

There are 10 types of people.
Those who know binary and those who don't.
I said: 'There are 10 types of people.'.
I also said: 'Those who know binary and those who don't.'.
Isn't that joke so funny?! False
This is the left side of...a string with a right side.

三、访问字符串中的值

字符串是由多个字符组成的集合,对于字符串的访问方式主要有索引和切片两种。

1.索引

可以对字符串建立索引(下标),第一个字符的索引为0。一个字符只是一个大小为一的字符串:

>>> word = 'Python'
>>> word[0]  # 位置下标为0的字符是"P"
'P'
>>> word[5]  # 位置下标为5的字符是"n"
'n'

索引也可以是负数,从右边开始计数:

>>> word[-1]  # 最后一个字符
'n'
>>> word[-2]  # 倒数第二个字符
'o'
>>> word[-6]
'P'

2.切片

除索引外,还支持切片。索引用于获取单个字符,切片允许您获取子字符串。

切片的格式为:变量名[起始位置下标(缺省时为0):结束位置下标(缺省时代表到字符串的结束位置)]

>>> word[0:2]  # 字符位置从0 (包含) to 2 (不包含)
'Py'
>>> word[2:5]  # 字符位置从2 (包含) to 5 (不包含)
'tho'

请注意始终包括开始,而不包括结束。这样可以确保始终:s[:i] + s[i:] = s

>>> word[:2] + word[2:]
'Python'
>>> word[:4] + word[4:]
'Python'

记住切片如何工作的一种方法是将索引视为指向字符之间的指针 ,第一个字符的左边缘编号为0。然后,一个由n个字符组成的字符串的最后一个字符的右侧边缘是索引n,例如:

 +---+---+---+---+---+---+
 | P | y | t | h | o | n |
 +---+---+---+---+---+---+
 0   1   2   3   4   5   6
-6  -5  -4  -3  -2  -1

数字的第一行给出索引0…6在字符串中的位置;第二行给出相应的负索引。从i到 j的切片由分别标记为ij的边之间的所有字符组成。

对于非负索引,如果切片的长度都在索引范围之内,则它们的长度就是索引的差。例如,长度word[1:3]为2。

尝试使用太大的索引将导致错误(意思是索引超过了所指向字符串的范围)

>>> word[42]  # 字符串中只有6个字符
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range

但是,超出范围的切片索引在用于切片时会得到妥善处理(切片时超过的范围会自动舍弃)

>>> word[4:42]
'on'
>>> word[42:]
''

敲黑板划重点:字符串是不可变的对象。如果通过索引或切片为其赋值将发生错误。

>>> word[0] = 'J'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>> word[2:] = 'py'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment

如果需要使用并修改、编辑字符串的值,则需要建立一个新的字符串。

下一章我们将介绍Python中与字符串相关的内部函数用法。

参考文献:

[1]https://docs.python.org/3/tutorial/introduction.html#strings

[2]Zed A.Shaw[US].Learn python the hard way(Third edition).Addison Wesley,2019(Page:30-31)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胡老师11452

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值