当前位置: 代码迷 >> 综合 >> 某马python day05
  详细解决方案

某马python day05

热度:46   发布时间:2023-12-13 09:22:57.0

python基础

字符串的认识和学习

  1. 字符串的认识和学习
#引号的个数和他们的区别表示
a='I ' \'am tom';
b="I am tom";
#三个引号的数据可以保存他们的格式,按照他们数据的格式进行输出
c='''I am tom''';
d="""I am tom""";
e='I\'m tom'
print(type(a));
print(type(b));
print(type(c));
print(type(d));
print(type(e));
print(a);
print(b);
print(c);
print(d);
print(e);
  1. 字符串的输入和输出的写法:
#字符串的输入和输出
name=input("请输入你的名字:");
print("我的名字是%s"%name);
print(f"你的名字是{
      name}");
  1. 字符串的下标:
    “下标“又叫“索引“,就是编号。比如火车座位号,座位号的作用:按照编号快速找到对应的座位。同
    理,下标的作用即是通过下标快速找到对应的数据。
    字符串的取出数据
#字符串的下标:他是从0号位置开始,总的长度是n-1,在取已知位置的数据的时候可以直接的使用下标去除数据
name='tomandjerry';
print(name[0]);
  1. 切片:切片是指对操作的对象截取其中一部分的操作。字符串、列表、元组都支持切片操作
#切片:切片是指对操作的对象截取其中一部分的操作。字符串、列表、元组都支持切片操作。
''' 切片的语法序列[开始位置下标:结束位置下标:步长] 注意事项: 1.不包含结束位置下标对应的数据,正负整数均可; 2.步长是选取间隔,正负整数均可,默认步长为1。 '''
name="0123456789";
#字符串可以直接的使用下表进行取出
print(name[2:5:1]);    #234
#从下标为2的地方开始,到下标为5的地方结束,最后的一个是数据的间隔。
print(name[2:5:2]);    #24
#当开始的位置省略是,默认为从第一个数据开始计算,结束的位置省略,则是一直截取到最后的位置,两者都省略,
#对于字符串不进行截取的操作,全部进行输出
print(name[:6:1]);
print(name[6:]);
print(name[:]);
#当截取的间隔是-1的时候,则数据逆向输出
print(name[::-1]);
#当选取的数据是负数的时候,则选取的方向则是从右往左开始选取
print(name[-4,-1,1]);
  1. 数据串的常用操作:查找
    (1).所谓字符串查找方法即是查找子串在字符串中的位置或出现的次数。
    (2).find():检测子串是否包含在这个字符串中,如果在返回这个子串开始的位置下标,否则返
    回-1。
    (3).字符串序列.find(子串,开始位置下标,结束位置下标)
    注意:开始和结束位置下标可以省略,表示在整个字符串序列中查找。
    (4).字符串的查找的代码和API的详细介绍
#字符串的查找:字符串序列.find(子串,开始位置下标,结束位置下标)
str="hello world and itcast and itheima and Python";
#查找数据的子串:
print(str.find("and"));
print(str.find("and",15,30));
print(str.find("ands"));
'''index():检测某个子串是否包含在这个字符串中,如果在返回这个子串开始的位置下标,否则则会报异常 注意:开始和结束位置下标可以省略,表示在整个字符串序列中查找。 '''
print(str.index("and"));
print(str.index("and",15,30));
print(str.index("ands"));
''' count():返回某个?串在字符串中出现的次数 注意:始和结束位置下标可以省略,表示在整个字符串序列中查找。 '''
print(str.count("and"));
  1. 数据串的常用操作:修改
''' 字符串的修改:所谓修改字符串,指的就是通过园数的形式修改字符串中的数据。 字符串序列.replace(旧子串,新子串,替换次数),通过程序的运行,replace()函数是有返回值的。 '''
print(str.replace("and","he"));

注意:数据按照是否能直接修改分为可变类型和不可变类型两种。字符串类型的数据修改的时候。不能改变原有字符串,属于不能直接修改数据的类型即是不可变类型。
7. 数据串的常用操作:分割
分割:按照指定字符分割字符串
分割的语法函数:字符串序列.split(分割字符, num),分割的子串是被丢弃的,不会再最终的结果中出现
代码展示:

'''字符串序列.split(分割字符, num) '''
str1="hello world and itcast and itheima and Python";
print(str1.split("and"));
# 函数运行结果:['hello world ', ' itcast ', ' itheima ', ' Python']
print(str1.split("and",2));
# 函数运行结果:['hello world ', ' itcast ', ' itheima and Python']
print(str1.split(" "));
# 函数运行结果:['hello', 'world', 'and', 'itcast', 'and', 'itheima', 'and', 'Python']
print(str1.split(" ",2))
# 函数运行结果:['hello', 'world', 'and itcast and itheima and Python']
  1. 数据串的常用操作:合并
    合并join():??个字符或?串合并字符串,即是将多个字符串合并为?个新的字符串
    合并的函数语法:字符或?串.join(多字符串组成的序列)
    代码展示:
""" 字符或?串.join(多字符串组成的序列) """
list1=["wo","love","python"];
t1=("a","b","c");
print("...".join(list1));
# 运行结果:wo...love...python
print("_".join(t1));
# 运行结果:a_b_c
  1. 数据串的常用操作:字符串的操作
    capitalize():将字符串第?个字符转换成?写
    title():将字符串每个单词?字?转换成?写
    lower():将字符串中?写转?写。
    upper():将字符串中?写转?写
    各个函数的执行和结果如下:
''' 字符串的操作:capitalize()title()lower()lstrip()rsplit()split()ljust():返回?个原字符串左对?,并使?指定字符(默认空格)填充?对应?度 的新字符串。 '''
str1=" hello world and itcast and itheima and Python ";
print(str1.capitalize());  #hello world and itcast and itheima and python
print(str1.title());  #Hello World And Itcast And Itheima And Python 
print(str1.lower());  #hello world and itcast and itheima and python 
print(str1.lstrip());  #hello world and itcast and itheima and Python 
print(str1.rsplit());  #['hello', 'world', 'and', 'itcast', 'and', 'itheima', 'and', 'Python']
print(str1.split());  #['hello', 'world', 'and', 'itcast', 'and', 'itheima', 'and', 'Python']
  1. 数据串的常用操作:判断

startswith():检查字符串是否是以指定?串开头,是则返回 True,否则返回 False。如果设置开始和结束位置下标,则在指定范围内检查。
endswith()::检查字符串是否是以指定?串结尾,是则返回 True,否则返回 False。如果设置开始和结束位置下标,则在指定范围内检查。
isalpha():如果字符串?少有?个字符并且所有字符都是字?则返回 True, 否则返回 False。
isdigit():如果字符串只包含数字则返回 True 否则返回 False
isalnum():如果字符串?少有?个字符并且所有字符都是字?或数字则返 回 True,否则返回False。
isspace():如果字符串中只包含空?,则返回 True,否则返回 False
字符串的语法:
字符串序列.startswith(?串, 开始位置下标, 结束位置下标)
字符串序列.endswith(?串, 开始位置下标, 结束位置下标)

''' startswith():检查字符串是否是以指定?串开头,是则返回 True,否则返回 False。如果设置开始和结束位置下标,则在指定范围内检查。 endswith()::检查字符串是否是以指定?串结尾,是则返回 True,否则返回 False。如果设置开始和结束位置下标,则在指定范围内检查。 isalpha():如果字符串?少有?个字符并且所有字符都是字?则返回 True, 否则返回 False。 isdigit():如果字符串只包含数字则返回 True 否则返回 False isalnum():如果字符串?少有?个字符并且所有字符都是字?或数字则返 回 True,否则返回False。 isspace():如果字符串中只包含空?,则返回 True,否则返回 False '''
print(str1.startswith('hello'));  #False
print(str1.startswith('hello',5,20));  #False
print(str1.isalpha());  #False
print(str1.isdigit());  #False
print(str1.isalnum());  #False
print(str1.isspace());  #False