字符串的切片和函数

引号

s1 = 'abc'
s2 = "abc"
s3 = '''abc'''

三引号占用的内存空间与单双引号的相同

==比较的是内容
is比较的是地址

print(id(s1),id(s2),id(s3))

print(s1 == s2)
print(s1 is s3)

当使用input输入时,底层做了处理,所以最后的地址是不一样的

s4 = input('请输入: ')
s5 = input('请输入: ')
print(s4 == s5)
print(s4 is s5)
请输入: abc
请输入: abc
True
False

字符串运算符:

+*

+拼接
*倍数

print(s1+s2)
print('-----')
print(s1*3)
abcabc
-----
abcabcabc

in 在...里面

name = 'steven'

result = 'st' in name
print(result)

result = 'tv' not in name
print(result)
True
True

切片

字符串的位置,也叫做下标或者索引
位置从0开时

filename = 'picture.png'

只取一个字母

从0开时数,取第五位字符
012345
pictur

print(filename[5])
r

包前不包后

print(filename[0:7])
picture

截取字符串

print(filename[4:7])
ure

当省略后面的数,表示一直取到字符串的末尾

print(filename[8:])
png

负数

-1即最后一个字符的下标
-2是倒数第二个字符的下标,依次类推

print(filename[8:-1])
print(filename[:-2])
print(filename[-3:])
pn
picture.p
png

倒叙

需要指定冒号分隔的第三位的值为
1表示从左往右取值
-1表示从右往左取值

print(filename[::-1])
print(filename[10:7:-1])
print(filename[-1:-4:-1])
gnp.erutcip
gnp
gnp

步长

print(filename[::1])
print(filename[::2])
print(filename[::-2])
print(filename[::3])
picture.png
pcuepg
gpeucp
pten

函数

capitalize()

将字符串的第一个字符转换成大写的标识形式

s1 = 'she is free so she has much free TIME.'
a = s1.capitalize()
print(a)
She is free so she has much free time.

title()

将字符串的每个单词的首字母转换为大写

a = s1.title()
print(a)
She Is Free So She Has Much Free Time.

istitle()

判断字符串的首字母是否大写,返回True,False

a = s1.title()
print(s1.istitle(),a.istitle())
False True

upper()

将字符串全部转换为大写

a = s1.upper()
print(a)
SHE IS FREE SO SHE HAS MUCH FREE TIME.

lower()

将字符串全部转换为小写

a = s1.lower()
print(a)
she is free so she has much free time.

len()

获取字符串的长度

print(len(s1))
38

find()

查找

s1 = 'she is free so she has much free time.'

返回值是-1,则代表没有找到

position = s1.find('z')
print(position)
-1

如果找到,则返回字符第一次出现的位置

position = s1.find('s')
print(position)
0

指定开始和结束位置

position = s1.find('s',11)
print(position)
12
position = s1.find('s',11,12)
print(position)
-1

rfind()

position = s1.rfind('free')
print(position)
28

replace()

替换

语法

replace(old,new[,max])

max为替换的最大次数

a = s1.replace('free','busy')
print(a)

a = s1.replace('free','busy',1)
print(a)
she is busy so she has much busy TIME.
she is busy so she has much free TIME.

encode()和decode()

编码 encode
解码 decode

msg = '上课啦! 认真听课!'
result = msg.encode('utf-8')
print(result)
m = result.decode('utf-8')
print(m)
b'\xe4\xb8\x8a\xe8\xaf\xbe\xe5\x95\xa6! \xe8\xae\xa4\xe7\x9c\x9f\xe5\x90\xac\xe8\xaf\xbe!'
上课啦! 认真听课!

startswith()和endswith

startswith(),判断是否是以xxx开头的
endswith(),判断是否是以xxx结尾的
返回值都是布尔类型:True,False

filename = 'helloworld.txt'
result = filename.endswith('txt')
print(result)
result = filename.startswith('hello')
print(result)
True
True

isalpha

isalpha()是否是字母
isdigit()是否是数字

s1 = 'a'
s2 = '1'
a = s1.isalpha()
b = s2.isdigit()
print(a,b)
True True

发现'1',也会被识别为数字

案例:三个数累计求和

sum = 0
i = 1
while i <= 3:
    n = input('请输入第{}个数字: '.format(i))
    if n.isdigit():
        n = int(n)
        sum += n
        print('第{}个数字累加成功! '.format(i))
        i += 1
    else:
        print('{}不是数字,请重新输入'.format(n))
print('sum=',sum)
请输入第1个数字: 1
第1个数字累加成功!
请输入第2个数字: 2
第2个数字累加成功!
请输入第3个数字: a
a不是数字,请重新输入
请输入第3个数字: 3
第3个数字累加成功!
sum= 6

join()

拼接字符串

将abc用-连接构成一个新的字符串

new_str = '-'.join('abc')
print(new_str,type(new_str))
a-b-c <class 'str'>

输出列表时,指定分割符

list1 = ['a','v','0','9']
result = ''.join(list1)
print(result)

result = ' '.join(list1)
print(result)
av09
a v 0 9

strip()

lstrip()去除字符串左侧的空格
rstrip()去除字符串右侧的空格
strip()去除字符串两侧的空格

s1 = '  a b  '
a = s1.lstrip()
print('*'+a+'*')
a = s1.rstrip()
print('*'+a+'*')
a = s1.strip()
print('*'+a+'*')
*a b  *
*  a b*
*a b*

split()

分割字符串
将分割后的字符保存到列表中
指定分隔符为空格

s = 'she is free so she has much free time.'
result = s.split(' ')
print(result,type(result))
['she', 'is', 'free', 'so', 'she', 'has', 'much', 'free', 'time.'] <class 'list'>

count()

获取字符串中指定字符的数量

获取字符串中一共有多少字符

a = s.count('')
print('个数',a)
个数 39

获取字符串中指定字符的数量

a = s.count(' ')
print('个数',a)

a = s.count('she')
print('个数',a)
个数 8
个数 2

猜你喜欢

转载自www.cnblogs.com/inmeditation/p/12291163.html