话不多说,上代码,看结果。
print('1、###########################################################')
print('一对单引号')
print('''三对单引号''')
print("一对双引号")
print("""三对双引号""")
print('asdf\t1234')
print(r'asdf\t1234')
print('2、###########################################################')
print('1、原子弹是个大\'烟花\'')
print("2、原子弹是个大'烟花'")
juZi = "3、原子弹是\
个大'烟花'"
print(juZi)
juZi = ("4、原子弹是个大"
"'烟花'")
print(juZi)
print('''123
456
789''')
print('3、字符串操作-拼接###########################################################')
print('5、原子弹是个大' + '烟花')
print('6、原子弹是个大''烟花')
print('7、原子弹%s"烟花"' % '是个大')
print('我%s你' % ('爱' * 3))
print('4、字符串操作-切片###########################################################')
str = '0123456789'
print(str[0])
print(str[-1])
str = '0123456789'
print(str[2:9:2])
print(str[2:len(str):2])
print(str[-1:-8: -2])
print(str[:: -1])
print('5、字符串操作-函数-查找###########################################################')
str = '0123456789'
print('计算字符串的字符个数 len(x):', len(str))
str = 'i want a new computer'
print('查找子串索引位置:', str.find('a', 1, 9))
print(str.find('a', 1, 4))
print(str.find('z'))
print(str.rfind('e'))
str = 'i want a new computer'
print('获取子串索引位置:', str.index('want'))
print(str.index('a'))
print(str.rindex('er'))
print(str.rindex('a'))
str = 'i want a new computer'
print('计算某个子字符串的出现个数:', str.count("an"))
print(str.count("z"))
print('6、字符串操作-函数-转换###########################################################')
str = 'i want a new computer'
print(str.replace('a', 'b', 1))
print(str.replace('a', 'b', 2))
print(str.replace('e', 'xxx'))
str = 'i want a new computer'
print(str.capitalize())
str = 'i want a new computer'
print(str.title())
str = 'I Want a NEW computer'
print(str.lower())
str = 'i want a new computer'
print(str.upper())
print('7、字符串操作-函数-填充压缩###########################################################')
str = '0123456789'
print(str.ljust(13, 'x'))
print(str.ljust(3, 'x'))
print(str.rjust(13, 'x'))
print(str.center(14, 'x'))
str = 'i want a new computer'
print('移除:', str.lstrip('i'))
print(str.rstrip('er'))
print('8、字符串操作-函数-分割拼接###########################################################')
str = 'xxx-21-921-927'
a = str.split('-', 2)
print(a, type(a))
str = 'xxx-21-921-927'
a = str.partition('-')
b = str.partition('/')
print(a, type(a), b, type(b))
a = str.rpartition('-')
b = str.rpartition('/')
print(a, type(a), b, type(b))
str = 'i want\r a new\n computer'
print(str.splitlines(True), type(str.splitlines(True)))
print(str.splitlines(False), type(str.splitlines(False)))
str = '0123456789'
print('x'.join(str), type('x'.join(str)))
print('9、字符串操作-函数-判定###########################################################')
print('boy'.isalpha(), 'boy123'.isalpha())
print('boy123'.isdigit(), '123'.isdigit())
print('boy123'.isalnum(), 'boy123/*-+'.isalnum())
print(' \n '.isspace(), ' 12'.isspace())
print('wo shi shui'.startswith('wo', 0, 7), 'wo shi shui'.endswith('wo', 0, len('wo shi shui')))
print('shi' in 'wo shi shui', 'wo' not in 'wo shi shui')
结果如下图。 就先这样,遇到别的再补充。
|