龙空技术网

python关于str(字符串)的代码,和面试常考题:is和==的区别

虎纹同学 237

前言:

此时小伙伴们对“python转str”大概比较看重,看官们都需要剖析一些“python转str”的相关文章。那么小编也在网摘上搜集了一些关于“python转str””的相关内容,希望看官们能喜欢,兄弟们快快来了解一下吧!

str(字符串)是不可变数据类型,即更改字符串内容,储存地址会发生改变,但原来的字符串内容不会发生改变

目录:增,删,改,查,逻辑符号

代码 功能 例子

拼接符 + 将两个字符串拼接 str1 + str2 + str3

格式化占位符%s 将两个字符串拼接 ' %s %s %s ' %(str1, str2, str3)

join 将两个字符串拼接 '连接符'.join([str1, str2, str3])

format 将两个字符串拼接 ' {} {} {} '.format(str1, str2, str3)

前面花括号设置字符串顺序, ' {0} {1} {2} '.format(str1, str2, str3)

输入元素在字符串里的下标位置 '{1} {0} {2} '.format(str1, str2, str3)

自定义字符串顺序 '{n1} {n2} {n3}'.format(n1 = str2, n2 = str3, n3 = str1)

f’{str1}{str2}{str3}‘

#字符串拼接

拼接符+

a = 'hello'b = ' world'c = '!'print(a+b+c)

hello world!

2.‘%s%s%s' %(str1, str2, str3)

a = 'hello'b = ' world'c = '!'d = '%s%s%s'%(a,b,c)print(d)

hello world!

3.'连接符 '.join([str1, str2, str3])

a = 'hello'b = ' world'c = '!'d = '_'.join([a,b,c])print(d)

hello_ world_!

4.format

'{}{}{}'.format(str1,str2,str3)

a = 'hello'b = ' world'c = '!'d = '{}{}{}'.format(a,b,c)print(d)

hello world!

'{0}{1}{2}'.format(str1,str2,str3)

a = 'hello'b = ' world'c = '!'d = '{0}{1}{2}'.format(a,b,c)e = '{0}{2}{1}'.format(a,b,c)print(d)print(e)

hello world!

hello! world

'{n1}{n2}{n3}'.format(n1=str1,n2=str2,n3=str3)

a = 'hello'b = ' world'c = '!'d = '{n3}{n1}{n2}'.format(n1=a,n2=b,n3=c)print(d)

!hello world

f’{str1}{str2}{str3}‘

a = 'hello world'b = '!'c = f'{a,b}'print(c)

('hello world', '!')

代码 功能 例子

replace() 替换字符串内容 str_1 = 'hello'

print(str_1.replace('hello','hi'))

strip() 删掉字符串两端空格 a.strip()

lstrip() 删掉字符串左边空格 a.lstrip()

rstrip() 删掉字符串右边空格 a.rstrip()

#替换字符串内容 replace()

str_1 = 'hello'print(str_1.replace('hello','hi'))

hi

若替换为空格,则是删除原来的

a = 'asd fgh 123'print(a.replace(' ',''))

asdfgh123

#横向制表符\t, 换行\n

a = 'hello\tworld'b = 'hello\nworld'print(a)print(b)

hello world

hello

world

#删掉字符串空格, 可删掉字符串两端的\t,\n

strip() 删掉字符串两端空格

lstrip() 删掉字符串左边空格

rstrip() 删掉字符串右边空格

a = '  au11k1asdf  'print(a.strip())print(a.lstrip())print(a.rstrip())

au11k1asdf

au11k1asdf

au11k1asdf

改,不能改变原来的字符串,只能临时改

代码 功能 例子

capitilize() 第一个字母大写 a.capitalize()

title() 每个单词首字母大写 a.title()

upper() 全部字母大写 a.upper()

lower() 全部字母小写 a.lower()

split() 按规定的元素切割字符串 a.split('1')

可设置切割的次数 a.split('1',2)

#改字母大小写

1.capitilize() 第一个字母大写

a = 'hello\tworld'b = 'hello\nworld'print(a)print(b)

Asd fgh 123

2.title() 每个单词首字母大写

a = 'asd fgh 123'print(a.title())

Asd Fgh 123

3.upper() 全部字母大写

a = 'asd fgh 123'print(a.upper())

ASD FGH 123

4.lower() 全部字母小写

a = 'asd fgh 123'print(a.lower())

asd fgh 123

#split() 按规定的元素切割字符串,可设置切割的次数

a = 'au11k1asdf'print(a.split('1'))print((a.split('1',2)))

['au', '', 'k', 'asdf']

['au', '', 'k1asdf'] (因为在split后设置了2,表示只切割到第二个‘1’,第三个‘1’没有切到)

代码 功能 例子

find() 检测一个str是否在另一个str里, a = 'asdf'

在返回索引位置,不在则返回-1 a.find('f')

index() 同上,不在时报异常 a = 'asdf'

a.index('f')

rfind() 类似于find,只不过是从右边找 a = 'asdf'

a.rfind('f')

rindex() 类似于index,只不过是从右边找 a = 'asdf'

a.rindex('f')

count() 统计指定str在指定位置之间出现的次数 a = 'asdf'

a.count(指定str,起始位置,终止位置)

in和not in 判断一个str是否在另一个str里 a = 'asdf'

在返回True,不在则返回False 's' in a 或者' s' not in a

== 判断两边数据是否一样 a == b

is 1.判断两边数据是否一样 a is b

2.判断两边数据的id是否一致

面试常考题:==和is的区别

==:比较两边的值是否相同

is:1.比较两边的值是否相同;2.比较两边的id地址是否一样

查询id地址:id()

在交互环境下(命令窗口),若两个数值在[-5,256]之间,且两个值相等,那么他们的id地址也相等。在pycharm里面不考虑数值是否在这个范围

交互环境模式下,is的比较

pycharm下,is的比较

逻辑符号

and--与     两边条件为真(True),则为真or----或     一边条件为真,就为真not---非     满足为假,不满足为真运行顺序:not-and-or

数值在布尔值(bool)下代表的意思:0为假(False),其余为真(True)

print(1 and not 0 or 0)

True

解析:首先判断 not 0,非假即真,所以返回True(1)

判断1 and 1,两真才真,所以返回True(1)

最后判断1 or 0,一真即真,所以返回True(1)

标签: #python转str