6、Python 数据类型详细篇:字符串

字符串类型的数据表示一段文本,使用单引号或者双引号创建:

  • 单引号字符串 ‘hello’
  • 双引号字符串 “world”
>>> x = 'hello'
>>> x
'hello'
>>> y = "world"
>>> y
'world'

使用单引号或者双引号创建的字符串只能在一行,而使用三引号允许一个跨多行的字符串。

使用 3 个单引号创建的多行字符串示例如下:

s = '''line 1
line 2
line 3'''
print(s)

使用 3 个双引号创建的多行字符串示例如下:

s = """line 1
line 2
line 3"""
print(s)

以上程序的输出如下:

line 1
line 2
line 3

常见运算操作

运算符 +

使用运算符 + 连接两个字符串,示例如下:

>>> 'Hello' + 'World'
>>> 'HelloWorld'
>>> 'Hello' + ' ' + 'World'
>>> 'Hello World'

运算符 *

使用运算符 * 重复输出字符串,示例如下:

>>> 'hello' * 2
'hellohello'
>>> 'hello' * 3
'hellohellohello'

函数 len

使用函数 len 获取字符串的长度,示例如下:

>>> len('abc')
3
>>> len('imooc')
5

索引 []

通过索引 [] 获取字符串中指定位置的字符,示例如下:

>>> s = 'imooc'
>>> s[0]
'i'
>>> s[1]
'm'
>>> s[2]
'o'
>>> s[3]
'o'
>>> s[4]
'c'

索引 [:]

使用语法 string [start:end],获取字符串 string 中在 [start, end) 范围的子字符串。注意范围 [start, end) 包含 start,不包含 end。举例如下:

>>> s = 'imooc'
>>> s[1]
'm'
>>> s[2]
'o'
>>> s[3]
'o'
>>> s[1:4]
'moo'

关键字 in

通过关键字 in 检查字符串中是否包含指定字符串,示例如下:

>>> 'mooc' in 'imooc'
True
>>> 'mook' not in 'imooc'
True

常用字符串方法

capitalize () 方法

capitalize () 方法把字符串的第一个字符大写,示例如下:

>>> text = 'abc'
>>> text.capitalize()
'Abc'

count () 方法

count () 方法统计字符串出现的次数,示例如下:

>>> text = 'abc abc'
>>> text.count('abc')
2

startswith (target) 方法

startswith (target) 方法检查字符串是否是以字符串 target 开头,示例如下:

>>> text = 'abc'
>>> text.startswith('ab')
True
>>> text.startswith('bb')
False

endswith (target) 方法

endswith (target) 方法检查字符串是否是以字符串 target 结尾,示例如下:

>>> text = 'abc'
>>> text.endsswith('bc')
True
>>> text.endsswith('cc')
False

lower () 方法

lower () 方法将字符串中所有大写字符转换为小写,示例如下:

>>> text = 'ABC'
>>> text.lower()
'abc'

upper () 方法

upper () 方法将字符串中所有小写字符转换为大写,示例如下:

>>> text = 'abc'
>>> text.upper()
'ABC'

islower () 方法

如果字符串中所有字符是小写则为真,否则为假,示例如下:

>>> text = 'abc'
>>> text.islower()
True

isupper () 方法

如果字符串中所有字符是大写则为真,否则为假,示例如下:

>>> text = 'ABC'
>>> text.isupper()
True

find (target) 方法

检查是否包含指定字符串 target,如果包含字符串 target 则返回开始的索引值,否则返回 -1,示例如下:

>>> text = 'imooc'
>>> text.find('oo')
2
>>> text.find('oop')
-1

split () 方法

使用空格将字符串分割为多个单词,返回一个列表,示例如下:

>>> text = 'hello world'
>>> text.split()
['hello', 'world']

缺省情况下,使用空格将字符串分割为多个单词。也可以在 split () 方法中指定分隔符,示例如下:

>>> text = 'hello:world'
>>> text.split(':')
['hello', 'world']

转义字符

>>> print('a\tb\tc')
a       b       c
>>> print('aa\tbb\tcc')
aa      bb      cc
>>> print('a\nb')
a
b
>>> print('\'')
'
>>> print("\"")
"
>>> print('\\')
\

字符串格式化

format 方法

>>> name = 'tom'
>>> city = 'beijing'
>>> text = 'My name is {}, I live in city {}.'
>>> text.format(name, city)
'My name is tom, I live in beijing.'

% 运算符

>>> name = 'tom'
>>> city = 'beijing'
>>> text = 'My name is %s, I live in city %s.'
>>> text % (name, city)
'My name is tom, I live in beijing.'

在实践中,通常直接将字符串与参数使用运算符 % 格式化,如下所示:

>>> name = 'tom'
>>> city = 'beijing'
>>> 'My name is %s, I live in city %s.' % (name, city)
'My name is tom, I live in beijing.'

如果有多个参数,需要将所有的参数保存在一个元组中;如果只有一个参数,可以不用保存在元组中,举例如下:

>>> name = 'tom'
>>> 'My name is %s.' % name
'My name is tom.'

占位符

  1. % s 用于格式化字符串
>>> name = 'tom'
>>> 'name is %s' % name
'name is tom'
  1. % d 用于格式化整数
>>> integer = 123
>>> 'integer is %d' % integer
'integer is 123'
  1. % f 用于格式化浮点数
>>> float = 123.456
>>> 'float is %f' % float
'float is 123.456000'
  1. %% 用于表示字符 % 本身
>>> a = 3
>>> b = 2
>>> c = a % b
>>> '%d %% %d = %d' % (a, b, c)
3 % 2 = 1

在实践中,通常使用 print 方法输出将格式化后的字符串,示例如下:

>>> name = 'tom'
>>> city = 'beijing'
>>> print('My name is %s' % name)
My name is tom
>>> print('My name is %s, I live in city %s.' % (name, city))
My name is tom, I live in beijing.

参考资料

http://www.imooc.com/wiki/pythonlesson1/pythonstring.html

posted @ 2022-06-15 21:46  tiansz  阅读(94)  评论(0编辑  收藏  举报