#一对单引号
name='小明'
city='北京'
#三引号
name2= '''小王'''
say='''how are
you'''
name3= """ 小李"""
say1= """ how old
are you"""
#三引号支持换行
print('hello world')
name = 'Timi'
print('我的名字是%s' % name)
print(f'我的名字是{name}')
name = input('请输⼊入您的名字:')
print(f'您输⼊入的名字是{name}')
print(type(name))
password = input('请输⼊您的密码:')
print(f'您输入的密码是{password}')
print(type(password))
下标也就是索引,比如火车座位号,座位号的作用:按照编号快速找到对应的座位。同理,下标的作用是通过下标快速找到对应的数据。下标都是从0开始。
name = "abcdef"
print(name[1])
print(name[0])
print(name[2])
切片是指对操作的对象截取其中一部分的操作。字符串、列列表、元组都支持切片操作。
序列[开始位置下标:结束位置下标:步⻓长]
注意
name = "abcdefg"
print(name[2:5:1]) # cde
print(name[2:5]) # cde
print(name[:5]) # abcde
print(name[1:]) # bcdefg
print(name[:]) # abcdefg
print(name[::2]) # aceg
print(name[:-1]) # abcdef, 负1表示倒数第一个数据
print(name[-4:-1]) # def
print(name[::-1]) # gfedcba
所谓字符串查找方法即是查找子串在字符串中的位置或出现的次数。
语法:字符串序列.find(子串, 开始位置下标, 结束位置下标)
#开始和结束位置下标可以省略,表示在整个字符串序列中查
mystr = "hello world and itcast and itheima and Python"
print(mystr.find('and')) # 12
print(mystr.find('and', 15, 30)) # 23
print(mystr.find('ands')) # -1
字符串序列.index(⼦串, 开始位置下标, 结束位置下标)
#:开始和结束位置下标可以省略,表示在整个字符串序列中查找
mystr = "hello world and itcast and itheima and Python"
print(mystr.index('and')) # 12
print(mystr.index('and', 15, 30)) # 23
print(mystr.index('ands')) # 报错
字符串序列.count(⼦串, 开始位置下标, 结束位置下标)
#:开始和结束位置下标可以省略,表示在整个字符串序列中查找
mystr = "hello world and itcast and itheima and Python"
print(mystr.count('and')) # 3
print(mystr.count('ands')) # 0
print(mystr.count('and', 0, 20)) # 1
字符串序列.replace(旧⼦串, 新子串, 替换次数)
#替换次数如果查出⼦串出现次数,则替换次数为该⼦串出现次数
mystr = "hello world and itcast and itheima and Python"
# 结果:hello world he itcast he itheima he Python
print(mystr.replace('and', 'he'))
# 结果:hello world he itcast he itheima he Python
print(mystr.replace('and', 'he', 10))
# 结果:hello world and itcast and itheima and Python
print(mystr)
数据按照是否能直接修改分为可变类型和不可变类型两种。字符串类型的数据修改的时不能改变原有字符串,属于不能直接修改数据的类型即不可变类型
字符串序列列.split(分割字符, num)
#num表示的是分割字符出现的次数,即将来返回数据个数为num+1个
mystr = "hello world and itcast and itheima and Python"
# 结果:['hello world ', ' itcast ', ' itheima ', ' Python']
print(mystr.split('and'))
# 结果:['hello world ', ' itcast ', ' itheima and Python']
print(mystr.split('and', 2))
# 结果:['hello', 'world', 'and', 'itcast', 'and', 'itheima', 'and', 'Python']
print(mystr.split(' '))
# 结果:['hello', 'world', 'and itcast and itheima and Python']
print(mystr.split(' ', 2))
#如果分割字符是原有字符串中的子串,分割后则丢失该⼦串
list1 = ['chuan', 'zhi', 'bo', 'ke']
t1 = ('aa', 'b', 'cc', 'ddd')
# 结果:chuan_zhi_bo_ke
print('_'.join(list1))
# 结果:aa...b...cc...ddd
print('...'.join(t1))
mystr = "hello world and itcast and itheima and Python"
# 结果:Hello world and itcast and itheima and python
print(mystr.capitalize())
# capitalize()函数转换后,只字符串第⼀个字符大写,其他的字符全都小写
mystr = "hello world and itcast and itheima and Python"
# 结果:Hello World And Itcast And Itheima And Python
print(mystr.title())
mystr = "hello world and itcast and itheima and Python"
# 结果:hello world and itcast and itheima and python
print(mystr.lower())
mystr = "hello world and itcast and itheima and Python"
# 结果:HELLO WORLD AND ITCAST AND ITHEIMA AND PYTHON
print(mystr.upper())
mystr = "hello world and itcast and itheima and Python "
# 结果:True
print(mystr.startswith('hello'))
# 结果False
print(mystr.startswith('hello', 5, 20))
mystr = "hello world and itcast and itheima and Python"
# 结果:True
print(mystr.endswith('Python'))
# 结果:False
print(mystr.endswith('Python', 2, 20))
mystr1 = 'hello'
mystr2 = 'hello12345'
# 结果:True
print(mystr1.isalpha())
# 结果:False
print(mystr2.isalpha())
mystr1 = 'aaa12345'
mystr2 = '12345'
# 结果: False
print(mystr1.isdigit())
# 结果:False
print(mystr2.isdigit())
mystr1 = 'aaa12345'
mystr2 = '12345-'
# 结果:True
print(mystr1.isalnum())
# 结果:False
print(mystr2.isalnum())
mystr1 = '1 2 3 4 5'
mystr2 = ' '
# 结果:False
print(mystr1.isspace())
# 结果:True
print(mystr2.isspace())