'''strip()''' # inp_name = input("enter your name:") # print(str(len(inp_name)), "your name is %s" % inp_name.strip()) '''strip()''' '''split()''' # my_str = "hello world!" # str_list = my_str.split() # print(str_list) '''split()''' # strip(),rstrip(),lstrip() 修剪字符串 # lower(),upper() 转换为大小写 # startswith(),endswith() 判断字符串是否以括号内指定的字符开头或结束,返回TRUE或FALSE # url = 'https://www.baidu.com/' # print(url.startswith('http') and url.rstrip('/').endswith('com')) '''format()格式化输出''' # u_name = 'yyh' # u_age = 18 # print("My name is {name},and I'm {age} years old.".format(name=u_name, age=u_age)) # print("My name is {},and I'm {} years old".format("yyh",18)) # print("My name is {0},and I'm {1} years old".format("yyh", 18)) '''format()格式化输出''' '''split()和rsplit()''' # my_str = 'a/b/c/d' # str_list = my_str.split('/', 1) # 指定分割次数 # print(str_list) # str_list = my_str.rsplit('/', 1) # 从右往左分割 # print(str_list) '''split()和rsplit()''' '''join()''' # my_str = 'yyh' # joined_str = '-'.join(my_str) # print(joined_str) # my_list = ['y', 'an', 'y', 'u', 'h', 'ui'] # joined_list = ''.join(my_list) # print(joined_list) '''join()''' '''replace()''' # my_str = "My name is {name} " # "I'm {age} years old. " # "{name} is my good friend".format(name='yyh',age=18) # print(my_str.replace('yyh', 'jxx', 1)) # 可设置替换次数 '''replace()''' '''digit()''' # my_str = '1231231' # print(my_str.isdigit()) '''digit()''' ############################################################### '''find()和rfind()''' # 从左往右查找指定字符串第一次出现的位置,如果找到返回第一个字符的索引,否则返回-1,可设置起始,结束位置 # my_str = 'I love youve !' # print(my_str.find('ve')) '''find()''' '''index()和rindex()''' # 同find(),但找不到会报错,可设置起始,结束位置 # my_str = 'I love you' # print(my_str.index('ve')) '''index()''' '''count()''' # 统计字符在字符串中出现的次数,可设置起始,结束位置 # my_str = 'My name is YanYuHui, my age is 18' # print(my_str.lower().count('y')) '''count()''' '''center(),ljust(),rjust()''' # 字符串填充字符串,居中,左对齐,右对齐 # print('info of yyh'.center(30,'-')) # print('info of yyh'.ljust(30,'-')) # print('info of yyh'.rjust(30,'-')) '''center(),ljust(),rjust()''' '''expandtabs()''' # 修改制表符代表的空格数 # my_str = 'hello world haha xixi' # print(my_str) # print(my_str.expandtabs()) # print(my_str.expandtabs(16)) '''expandtabs()''' '''capitalize()''' # 修改字符串第一个字符大写 # my_str = '-i love you' # print(my_str.capitalize()) # my_str = 'i love you' # print(my_str.capitalize()) '''capitalize()''' '''swapcase()''' # 反转大小写 # my_str = 'i LOVE YOU' # print(my_str.swapcase()) '''swapcase()''' '''title()''' # 单词首字母大写 # my_str = 'dear friend i miss you ' # print(my_str.title()) '''title()''' ###################################不能检测浮点数 '''isdigit()''' # 检测字符串或字节是否只由数字组成 # num2 = b'2' # num3 = u'2' # num4 = '二' # num5 = 'Ⅱ' # my_list = [num2,num3,num4,num5] # for num in my_list: # print(num.isdigit()) '''isdigit()''' '''isdecimal''' # 检测字符串或字节是否只由十进制字符组成 # num3 = u'2' # num4 = '二' # num5 = 'Ⅱ' # my_list = [num3, num4, num5] # for num in my_list: # print(num.isdecimal()) '''isdecimal''' '''isnumeric''' # 检测字符串包括unicode,中文数字,罗马数字 # num3 = u'2' # num4 = '二一' # num5 = 'Ⅱ' # my_list = [num3, num4, num5] # for num in my_list: # print(num.isnumeric()) '''isnumeric''' ################################不能检测浮点数 '''isalnum()''' # 是否只包含数字和字母 # my_str = 'tonny123' # print(my_str.isalnum()) '''isalpha''' # 是否只包含字母 '''isdentifier''' # 是否是有效python的标识符,测试变量名是否合法 '''islower(),isupper()''' # 是否是纯小(大)写 '''isspace()''' # 检测是否全为空格 '''istitle()''' # 检测首字母是否都大写
以上是字符串的操作