学习python的day4之字符串

1.认识字符串

字符串的定义方法

a = '1.hello pyhton!'
print(a)
print(type(a))

a1 = '2.hello ' \
    'pyhton!'      #单引号回车会加上一个转义字符,程序才不会报错
print(a1)
print(type(a1))


b = "3.hello pyhton!"
print(b)
print(type(b))

c = '''4.hello python!'''
print(c)
print(type(c))

c1 = '''5.hello
 python!'''                  #三引号不用加上转义字符
print(c1)
print(type(c1))

d = """6.hello python!"""
print(d)
print(type(d))

f1 = 'i\'m as_scheduled'
print(f1)
f2 = "i'm as_scheduled"
print(f2)
f3 = '''i'm as_scheduled'''
print(f3)

2.字符串的输入输出

2.1输入

用input()接收用户从键盘输入的数据   注:input()接受的数据都是字符串

a = input('输入名字:')
print(type(a))

2.2输出

输出有两种格式

print(f'你的名字是:{a}')
print('你的名字是:%s' %a)

3.下标

3.1取出字符串中的某个位置的字符  注:下标从0开始

#取出字符串中某个字符  注:下标从0开始
str1 = 'abcdefg'
print(str1[3])

3.2取出字符串中的一段字符(这种操作称为切片) 语法:序列[开始下标位置:结束下标位置:步长](默认步长为1) 注:包含左边的下标但不包括右边的下标

str2 = 'abcdefg'
print(str2[2:5])
print(str2[:3])
print(str2[3:])
print(str2[2:7:2])

3.3负数下标

str3 = 'ABCDEFG'
print(str3[-3])
print(str3[-3:-1])
print(str3[-1:-3])          #没有输出结果是因为  选取的方向与步长的方向不一样   print(str3[-1:-3:1])
print(str3[-1:-3:-1])
print(str3[ : :-1])  #倒序输出

4.字符串的常用操作方法

4.1查找

find()检测某个子串是否在字符串中,若在则返回该子串的下标位置,不在返回-1 (rfind():与find()方法功能相同,但查找方向为右侧开始)

查找子串的位置或出现次数

语法: 序列名.find(子串,子串开始下标,子串结束下标) 默认为在整个字符串中查找

str1 = 'abcdefg'
a1 = str1.find('cd')
print(f'子串开始的下标位置是{a1}')
a2 = str1.find('de',2,5)
print(f'子串开始的下标位置是{a2}')
a3 = str1.find('adef')            #该子串不存在返回-1
print(f'子串开始的下标位置是{a3}')

a4 = str1.rfind('cd')
print(f'子串开始的下标位置是{a4}')

#index():检测某个子串是否包含在这个字符串中,如果在 返回这个子串开始的位置下标,不在则报异常 (rindex():与index()方法功能相同,但查找方向为右侧开始)

语法: 序列名.index(子串,子串开始下标,子串结束下标) 默认为在整个字符串中查找

str1 = 'abcdefg'
a1 = str1.index('cd')
print(f'子串开始的下标位置是{a1}')
a2 = str1.index('de',2,5)
print(f'子串开始的下标位置是{a2}')
a3 = str1.index('adef')            #该子串不存在程序报错
print(f'子串开始的下标位置是{a3}')
a4 = str1.rindex('cd')
print(f'子串开始的下标位置是{a4}')

count():返回某个子串在字符串中出现的次数

语法: 序列名.count(子串,子串开始下标,子串结束下标) 默认为在整个字符串中查找

str1 = 'hello and word and hello and python and welcome and as_scheduled'
print(str1.count('and'))
print(str1.count('and',10,30))
print(str1.count('ands'))     #在字符串中没有该字串,返回0

4.2修改

所谓修改字符串,就是通过函数的形式修改字符串中的数据

 replace():替换 注:该函数修改的是返回值,并没有修改原来的字符串(由此可知数据分为可变类型和不可变类型,字符串为不可变类型)

语法:序列名.replace(旧子串,新子串,替换次数) 注:替换次数如果查出子串出现次数,则替换次数为子串出现次数

str1 = 'hello and word and hello and python and welcome and as_scheduled'
print(str1.replace('and','or'))
print(str1.replace('and','or',3))
print(str1.replace('and','or',10))   #查找到的出现次数只有5次,而替换次数有10次,超出了出现次数,替换次数为出现次数

4.3分割与合并

#2.2   split():按照指定字符分割字符串   分割,返回的是一个列表,同时会丢失分割字符
#语法: 序列名.split(分割字符,num)  注:num表示分割字符出现的次数,即将来返回数据个数为num+1个
#str1 = 'hello and word and hello and python and welcome and as_scheduled'
#print(str1.split('and'))
#print(str1.split('and',3))

#2.3 join()--合并列表中的字符串为一个大字符串
#语法: 连接字符.join(列表名)
'''
list1 = ['hello','word','hello','python','welcome','as_scheduled']
str1 = ' and '.join(list1)
print(str1)
'''

4.4字母大小写转换

#2.4 capitalize():将字符串第一个字符转换成大写字母  注:只有第一个字母变成大写字母
#title():将字符串第一个单词首字母转换成大写字母  注:是每一个单词首字母变成大写字母
#lower():将字符串中的大写字母转换为小写字母
#upper():将字符串中的小写字母转换为大写字母
#lstrip():删除字符串左侧空白字符   注:只是字符串左侧的空白字符,不包括字符串中其他的空白字符
#rstrip():删除字符串右侧空白字符   注:只是字符串右侧的空白字符,不包括字符串中其他的空白字符
#strip():删除字符串两侧空白字符   注:只是字符串两侧的空白字符,不包括字符串中其他的空白字符
'''
str1 = 'hello and world and hello and pyThon and welcome and As_scheduled'
print(str1.capitalize())
print(str1.title())
str2 ='hello python!'
print(str2.upper())
str3 = 'HELLO PYTHON!'
print(str3.lower())
str4 = '     hello world!    '
print(str4.lstrip())
print(str4.rstrip())
print(str4.strip())
'''

4.5字符串对齐

# ljust():返回一个字符串左对齐,并使用指定字符(默认空格)填充置对应长度的新字符串
# 语法:序列名.ljust(长度,填充字符)
# rjust():返回一个字符串右对齐,并使用指定字符(默认空格)填充置对应长度的新字符串
# 语法:序列名.rjust(长度,填充字符)
# center():返回一个字符串中间对齐,并使用指定字符(默认空格)填充置对应长度的新字符串
# 语法:序列名.center(长度,填充字符)
'''
str = 'hello'
print(str.ljust(10))
print(str.ljust(10,'.'))
print(str.rjust(10))
print(str.rjust(10,'.'))
print(str.center(10))
print(str.center(10,'.'))
'''

4.6判断

#判断
#判断即为判断真假,返回布尔值
#staterswith():检查字符串是否以指定子串开头,如果设置了开始和结束位置的下标,则在该范围内查找
#语法: 序列名.staterswith(子串,开始位置,结束位置)
'''
str1 = 'hello world and i like python and my name is as_scheduled!'
print(str1.startswith('hello'))
print(str1.startswith('hello',10,20))
'''
'''
#endswith():检查字符串是否以指定子串结尾,如果设置了开始和结束位置的下标,则在该范围内查找
#语法: 序列名.staterswith(子串,开始位置,结束位置)
str2 = 'hello world and i like python and my name is as_scheduled!'
print(str2.endswith('as_scheduled!'))
print(str2.endswith('as_scheduled!',10,20))
'''

#isalpha():如果字符串至少有一个字符并且所有字符则返回true,反之返回false
#语法:序列名.isalpha()
'''
str1 = 'hello'
print(str1.isalpha())
#输出结果:true
str1 = 'hello!'
print(str1.isalpha())
#输出结果:false
'''
'''
#isdigit():如果字符串只包含数字则返回true反之返回false
str1 = '123'
print(str1.isdigit())
#输出结果:true
str1 = '123!'
print(str1.isdigit())
#输出结果:false
'''
#isalnum():如果字符串至少有一个字符且所有字符都是字母或数字则返回true,反之返回false
str1 = '123'
print(str1.isalnum())
#输出结果:true
str2 = 'abc'
print(str2.isalnum())
#输出结果:true
str3 = '123abc'
print(str3.isalnum())
#输出结果:true
str4 = '123!'
print(str4.isalnum())
#输出结果:false
str5 = 'abc!'
print(str5.isalnum())
#输出结果:false

#isspace():如果字符串全是非空字符返回true,反之返回false
str6 = '    '
print(str6.isspace())
#输出结果:true
str7 = '  !   '
print(str7.isspace())
#输出结果:false

 

posted @ 2020-09-15 00:39  as_scheduled  阅读(240)  评论(0编辑  收藏  举报