name=input('请输入字符串:')
print(name)
print(type(name))
运行结果
请输入字符串:qwer
qwer
即用input()函数来进行输入,print()函数进行输出。
str1='abcdef'
print(str1[0])
print(str1[1])
print(str1[0:2:1])
运行结果
a
b
ab
即从前往后下标从0开始依次增加也可以反过来从后往前下标从-1开始依次减小,字符串的切片操作str[开始位置下标:结束位置下标:步进]。
find()、rfind()
index()、rindex()
count()
1.find()、rfind()
str1='hello python and muxue and world'
a=str1.find('and',0,20)
print(a)
a=str1.find('and',0,10)
print(a)
a=str1.find('and')
print(a)
b=str1.rfind('a')
print(b)
运行结果
13
-1
13
23
即find()函数为查找第一个匹配数据的下标,当查不到是返回-1,rfind()从右往左进行查询返回第一个匹配数据下标。。都可指定范围。
2.index()、rindex()
str1='hello python and muxue and world'
c=str1.index('and',0,20)
print(c)
c=str1.index('and')
print(c)
c=str1.rindex('and')
print(c)
c=str1.index('and',0,10)
print(c)
运行结果
13
13
23
Traceback (most recent call last):
File "D:/pycharm项目/数值类型.py", line 8, in
c=str1.index('and',0,10)
ValueError: substring not found
即index()函数也为查询第一个匹配数据的下标,但与find()不同查不到时会返回异常。rindex()与rfind()也相同,但与rfind()不同查不到时会返回异常。都可指定范围。
3.count()
str1='hello python and muxue and world'
d=str1.count('and',0,20)
print(d)
d=str1.count('and')
print(d)
运行结果
1
2
即count()函数为查找匹配数据的个数可指定范围。
替换:replace()
str1='hello python and muxue and world'
str2=str1.replace('and','he',1)
print(str2)
print(str1)
str2=str1.replace('and','he')
print(str2)
运行结果
hello python he muxue and world
hello python and muxue and world
hello python he muxue he world
即replace(‘要替换的字符串’,‘替换为的字符串’,次数)函数在进行替换时可指定次数也可不指定默认为全部替换。
分割:split()
str1='hello python and muxue and world'
list1=str1.split('and')
print(list1)
list1=str1.split('and',1)
print(list1)
运行结果
['hello python ', ' muxue ', ' world']
['hello python ', ' muxue and world']
即split()函数在进行分割时是以字符串中某一个序列作为间隔,分割完成后会以列表的形式进行返回,分割的后的字符串等于分割次数加1。
连接:join()
list1=['aa','bb','cc','dd']
tuple2=('2025','1','25')
print('.'.join(list1))
print('-'.join(tuple2))
运行结果
aa.bb.cc.dd
2025-1-25
即join()函数把列表、元组中的数据和指定的的字符串进行连接。
capitalize()
title()
lower()
upper()
1.capitalize()
str = 'hello world and python'
str1=str.capitalize()
print(str1)
运行结果
Hello world and python
即capitalize()函数是将字符串第一个字符转换成大写,其他字符全部为小写。
2.title()
str = 'hello world and python'
str1=str.title()
print(str1)
运行结果
Hello World And Python
即title()函数是将字符串每个首字母转换成大写。
3.lower()
str = 'HEllo woRld and python'
str1=str.lower()
print(str)
运行结果
hello world and python
即lower()函数是将字符串中的大写转换为小写。
4.upper()
str = 'hello world and python'
str1=str.upper()
print(str1)
运行结果
HELLO WORLD AND PYTHON
即upper()函数是将字符串中的小写转换为大写。
lstrip()
rstrip()
strip()
str=' hello and python '
print(str)
l_str=str.lstrip()
r_str=str.rstrip()
s_str=str.strip()
print(l_str)
print(r_str)
print(s_str)
运行结果
hello and python
hello and python
hello and python
hello and python
即lstrip()、rstrip()、strip()顾名思义l(left),r(right)分变为删除左、右两侧空格,strip()为删除左右空格。
ljust()
rjust()
center()
str='python'
r_str=str.rjust(10,'$')
l_str=str.ljust(10,'$')
c_str=str.center(10,'$')
print(r_str)
print(l_str)
print(c_str)
运行结果
$$$$python
python$$$$
$$python$$
即ljust()、rjust()、center()这三个函数为填充函数有两个参数第一个是总长度、第二填充的符号。
starwith()
endswith()
str='hello python'
is_start=str.startswith('hello')
print(is_start)
is_end=str.endswith('on')
print(is_end)
运行结果
True
True
即starwith()、endswith()分别为判断字符串开头字符和结束字符函数,返回值为bool类型。
isalpha()
isdigit()
isalnum()
isspace()
str='hello python'
str1='123'
str2='qwer^&'
str3=' '
print(str.isalpha())
print(str1.isdigit())
print(str2.isalnum())
print(str3.isspace())
运行结果
False
True
False
True
即isalpha()是判断是否全为字母,isdigit()是否全为数字,isalnum()是否全为数字字母,isspace()是否仅有空格。返回值为bool类型。