说明:str 字符串类型
字符串是由单引号 '' , 双引号 " " , 三引号 "" " " "" ,引起来的字符组合。
任何类型可以被转换为字符串类型。
在字符串中使用单引号或双引号的方式,单引号被双引号包括,双引号被单引号包括 . . . 。
1.字符串的使用
name_0 = 'hello word'
name_1 = "hello word"
name_2 = """
hello
word
"""
name_3 = "I'm, ok'"
name_4 = 'A"B"C'
name_5 = """ "asd" 'asd' """
print ( name_0)
print ( name_1)
print ( name_2)
print ( name_3)
print ( name_4)
print ( name_5)
hello word
hello word
hello
word
I 'm, ok'
A "B" C
"asd" 'asd'
2.其他类型转字符串
使用str ( ) , 将内容括起来。
print ( str ( 123 ) )
print ( str ( 123.4 ) )
print ( str ( 'abc' ) )
print ( str ( [ 1 , 2 , 3 ] ) )
print ( str ( ( 'hello' , ) ) )
print ( str ( { 'a' : 123 , 'b' : 456 } ) )
123
123.4
abc
[ 1 , 2 , 3 ]
( 'hello' , )
{ 'a' : 123 , 'b' : 456 }
3.连接 与 复制
符号 作用 例 + 加号 连接字符串。 ‘a’ + ‘b’ = ‘ab’ * 乘号 复制字符串。 ‘a’ * 3 = ‘aaa’
str_0 = 'Hello word '
print ( str_0 + 'abc' )
print ( str_0 * 2 )
4.按索引取值
字符一个字符占一个索引,可从左到右正向取值,从右到左反向取值。
格式:字符串 [ 起始值:上限值:步长 ]
起始值:从这个值开始。
上限值:上限的位置,能获取到它的前一位数,不包含它。
步长:默认为正 1 ,可以为负数。
省略索引 :
省略第一个索引相当于使用索引 0 或从列表的开始处开始。
省略第二个索引相当于使用列表的长度,意味着切片直至列表的末尾。
name = 'hello word '
print ( name[ 0 ] )
print ( name[ 0 : 11 ] )
print ( name[ 0 : 30 ] )
print ( name[ 0 : ] )
print ( name[ : 11 ] )
print ( name[ - 11 : - 1 ] )
print ( name[ 0 : - 1 ] )
print ( name[ - 30 : - 1 ] )
print ( name[ - 1 : - 12 : - 1 ] )
h
hello word
hello word
hello word
hello word
hello word
hello word
hello word
drow olleh
5.遍历
name = 'hello word '
for i in name:
print ( i)
6.字符截取拼接
name = 'Hello word'
print ( name [ 0 : 1 ] + 'a' )
7.字符个数
len ( ) 获取字变量中元素的个数。
count ( ) 统计字符串中指定字符出现的个数,将指定统计某字符引起来放入函数的括号内 , 不指定为全部。
name = 'hello word'
print ( len ( name) )
a = 'aac aad aag aaf'
print ( a. count( '' ) )
print ( a. count( 'a' ) )
print ( a. count( 'a' , 0 , 6 ) )
a = ""
print ( a. count( "" ) )
print ( len ( a) )
8.成员运算
in 在
not in 不在
返回为布尔值
name = 'hello word'
print ( 'h' in name)
print ( 'a' not in name)
9.strip ( ) 移除
. strip ( ) 移除指定的首尾字符。
. lstrip ( ) 移除首部,指定的的字符。
. rstrip ( ) 移除尾部,指定的的字符。
不写都默认移除空字符。需要移除的字符用引号引起来,中间的无法移除。
name = ' hello word '
print ( name. strip( ) )
name = '###hello word@@@'
print ( name. strip( '#' ) )
print ( name. strip( '@' ) )
name = '###hello word###'
print ( name. lstrip( '#' ) )
print ( name. rstrip( '#' ) )
hello word
hello word @ @ @
# # # hello word
hello word # # #
# # # hello word
10.切分拼接
指定一个字符为点,进行切分,指定切割的字符不会保留,不写默认指定为空字符。
指定的字符必须存在字符串中。split ( ) 中用引号引起来 , 被切分的的字符串会以列表类型存放 , 指定什么作为分隔 ,该字符不保留。
可以指定切割的次数。
. split ( ‘ ’ , maxsplit = _ ) 从左
. rsplit ( ‘ ’ , maxsplit = _ ) 从右
'' . join ( ) 将列表多个字符串元素拼接成字符串。
引号内指定一个拼接符号,引号内默认不写则不使用分隔符。
a = 'abc and acg adf'
print ( a. split( ) )
a = '1 | 2 | 3| 4 | 5'
print ( a. split( '|' ) )
print ( a. split( '|' , maxsplit= 1 ) )
print ( a. split( '|' , maxsplit= 2 ) )
print ( a. rsplit( '|' , maxsplit= 1 ) )
print ( a. rsplit( '|' , maxsplit= 2 ) )
s = [ 'a' , '1' , 'b' , '2' ]
print ( '' . join( s) )
print ( '|' . join( s) )
[ 'abc' , 'and' , 'acg' , 'adf' ]
[ '1 ' , ' 2 ' , ' 3' , ' 4 ' , ' 5' ]
[ '1 ' , ' 2 | 3| 4 | 5' ] # 切一次
[ '1 ' , ' 2 ' , ' 3| 4 | 5' ] # 切两次
[ '1 | 2 | 3| 4 ' , ' 5' ] # 从右边切一次
[ '1 | 2 | 3' , ' 4 ' , ' 5' ] # 从右边切一次
a1b2 # 无缝拼接
a | 1 | b | 2 # 指定分隔符拼接
11.字母大小转换
英文字母大小转换:
. upper 全转换为大写。
. isupper 检查是否全部为大写 , 返回值为布尔值。
. lower 全转换为小写。
. islower 检查是否全部是小写 , 返回值为布尔值。
. title ( ) 将所有英文单词首字母转为大写,其他全部转小写
. capitalize ( ) 将首字母大写,后面统一小写
a = 'aBc aNd aCg aDf'
b = a. upper( )
print ( b, b. isupper( ) )
a = b. lower( )
print ( a, a. islower( ) )
a = 'aac aad aag aaf'
print ( a. title( ) )
a = 'aAc aAd aAg aAf'
print ( a. capitalize( ) )
ABC AND ACG ADF True
abc and acg adf True
Aac Aad Aag Aaf
Aac aad aag aaf
12.检查字符串首尾
. startswith 检查字符串以开头xxx开头。
. endswith 检查字符串是否以xxx结尾。
返回值为布尔值。
a = 'aBc aNd aCg aDf'
print ( a. startswith( 'aB' ) )
print ( a. endswith( 'f' ) )
13.format( ) 传值
字符串格式化输出按照个占位个数依次传值。
方式 1 :花括号 { 空 } 占位 依次传值。
方式 2 :花括号 { 索引 } 占位 索引补充。
a = 'a{}c d{}f g{}i'
print ( a. format ( 'b' , 'e' , 'h' ) )
b = 'a{2}c d{1}f g{0}i'
print ( a. format ( 'b' , 'e' , 'h' ) )
abc def ghi
ahc def gbi
14.字符判断
. isalpha ( ) 判断是否纯字符,空字符,特殊符号不是字符,
. isdigit ( ) 判断是否为纯数字
name = 'a啊o哦'
print ( name. isalpha( ) )
a = '123a'
b = '123'
print ( a. isdigit( ) )
print ( b. isdigit( ) )
15.replace( ) 替换
在括号内指定替换的字符和替换的字符,用引号引起来,用逗号分隔。
a = 'aac aad aag aaf'
print ( a. replace( 'a' , 'c' ) )