Python-字符串
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 print("----字符串学习----") 4 # 认识字符串 5 a = 'hello world' # 'I\'m is Tom' 单引号 6 print(a, type(a)) 7 b = "Tom" # 双引号 8 print(a, type(b)) 9 c = '''i am Tom''' # 三引号 10 print(c, type(c)) 11 d = '''I 12 am Tom''' 13 print(d, type(d)) 14 15 # 字符串的输出 16 print("hello world") 17 name = 'Tom' 18 print('我的名字是: %s' % name) 19 print(f'my name is {name}') 20 21 # 字符串的输入,所有输入都是字符串 22 password = input('请输入密码:') 23 print(f'你输入的密码是:{password}', type(password)) 24 25 # 字符串下标,又叫索引 26 str1 = 'abcdefg' # 定义的字符串存在内存上 27 print(str1[0], str1[3], str1[-2]) 28 29 # 字符串的切片,格式:变量名[开始位置下标:结束位置下标:步长],步长为负数,表示重右至左 30 str2 = '012345678' 31 print(str2[0:2], str2[0:5:2], str2[:4], str2[5:], str2[-4::-1], str2[-4:-1:-1]) # 最后一个是无法切片出数据 32 33 # 字符串常用操作方法:查找(子串在字符串中出现的位置和次数) 34 mystr = 'hello world and itcast and itheime and Python' 35 # find(子串,查找起始位置,查找结束位置)查找子串下标,若查找不到,则返回-1。rfind()方法则是从右边开始查找 36 print(mystr.find('and'), mystr.find('and', 15, 30), mystr.find('ands')) 37 # index(子串,查找起始位置,查找结束位置)查找子串的下标。若查找不到,则报错。用法同find()。rindex()方法则是从右边开始查找 38 print(mystr.find('and'), mystr.find('and', 15, 30)) 39 # count(子串,查找起始位置,查找结束位置),查找子串出现次数。若查找不到,则返回0 40 print(mystr.count('and', 15, 30), mystr.count('and'), mystr.count('ands')) 41 42 # 字符串常用操作方法:修改(通过函数的形式修改字符串中的数据) 43 mystr1 = 'hello world and itcast and itheime and Python' 44 # replace(旧子串,新子串,替换次数),替换并不能改变原字符串,返回一个新的字符串 45 new_mystr1 = mystr1.replace('and', 'he') 46 print(mystr1, new_mystr1) 47 print(mystr1.replace('and', 'he', 1)) 48 # split(分隔符,分隔符出现的次数)。返回一个列表,丢失分隔符 49 list1 = mystr1.split('and') 50 list2 = mystr1.split('and', 2) 51 print(list1, list2) 52 # join(多字符串组成的序列)用一个字符串或子串合并字符串,即多个字符串合并成一个新的字符串 53 mylist = ['aa', 'bb', 'cc'] 54 new_str = '...'.join(mylist) 55 print(new_str, '...'.join('dd')) 56 # 字符串修改非重点函数 57 print(mystr1.capitalize()) # 将字符串的第一个字符装换为大写,其余全部小写 58 print(mystr1.title()) # 将字符串每个单词首字母装换成大写 59 print(mystr1.lower()) # 将字符串中的大写转换为小写 60 print(mystr1.upper()) # 将字符串中的小写转换为大写 61 mystr2 = ' hello world and itcast and itheime and Python ' 62 print(mystr2.lstrip()) # 删除字符串左侧空白字符 63 print(mystr2.rstrip()) # 删除字符串右侧空白字符 64 print(mystr2.strip()) # 删除字符串两侧空白字符 65 mystr3 = 'hello' 66 print(mystr3.ljust(10), mystr3.ljust(10, '.')) # 左对齐,不够默认填充空格,也可指定填充符 67 print(mystr3.rjust(10), mystr3.rjust(10, '.')) # 右对齐,不够默认填充空格,也可指定填充符 68 print(mystr3.center(10), mystr3.center(10, '.')) # 中间对齐,不够默认填充空格,也可指定填充符 69 70 # 字符串常用操作方法:判断(判断真假,True or False) 71 mystr4 = 'hello world and itcast and itheime and Python' 72 # startswith(子串,开始位置下标,结束位置下标),检查字符串是否以指定的子串开头,是则True,否则False 73 print(mystr4.startswith('hell'), mystr4.startswith('or', 7, 15), mystr4.startswith('oo', 7, 15)) 74 # endswith(子串,开始位置下标,结束位置下标),检查字符串是否以指定的子串结尾,是则True,否则False 75 print(mystr4.endswith('hon'), mystr4.endswith(' a', 5, 13), mystr4.endswith('aa', 5, 13)) 76 # isalpha() 如果字符串至少有一个字符并且所有字符都是字母则返回True,否则返回False 77 print('hello'.isalpha(), 'hell123'.isalpha()) 78 # isdigit() 如果字符串只包含数字则返回True,否则返回False 79 print('hello'.isdigit(), '123'.isdigit()) 80 # isalnum() 如果字符串至少有一个字符并且所有字符都是字母或者数字返回True,否则返回False 81 print('hello123'.isalnum(), '123*jj'.isalnum()) 82 # isspace() 如果字符串中只包含空白,则返回True,否则返回False 83 print('hello 123'.isspace(), ' '.isspace())
运行结果:
D:\Python\Python学习\venv\Scripts\python.exe D:/Python/Python学习/Python_1/__init__.py ----字符串学习---- hello world <class 'str'> hello world <class 'str'> i am Tom <class 'str'> I am Tom <class 'str'> hello world 我的名字是: Tom my name is Tom 请输入密码:123 你输入的密码是:123 <class 'str'> a d f 01 024 0123 5678 543210 12 23 -1 12 23 1 3 0 hello world and itcast and itheime and Python hello world he itcast he itheime he Python hello world he itcast and itheime and Python ['hello world ', ' itcast ', ' itheime ', ' Python'] ['hello world ', ' itcast ', ' itheime and Python'] aa...bb...cc d...d Hello world and itcast and itheime and python Hello World And Itcast And Itheime And Python hello world and itcast and itheime and python HELLO WORLD AND ITCAST AND ITHEIME AND PYTHON hello world and itcast and itheime and Python hello world and itcast and itheime and Python hello world and itcast and itheime and Python hello hello..... hello .....hello hello ..hello... True True False True True False True False False True True False False True Process finished with exit code 0

浙公网安备 33010602011771号