python中的字符串

  • 字符串的定义
a = "hello"
b = 'westos'
c = "what's up"
d = 'what\'s up'
e = """
        用户管理系统
        1.添加用户
        2.删除用户
        3.显示用户
"""


print(a)
print(b)
print(c)
print(d)
print(e)
print(type(e))

  • 字符串的特性
  • 索引: 0,1,2,3,4 索引值默认从0开始
s = 'hello'

print(s[1])   
print(s[0])   
运行结果:
e
h
  • 切片
print(s[0:3])   #切片的规则: s[start:end:step] 从start开始,到end-1结束,步长:step

print(s[0:4:2]) 
运行结果为:
hel
hl
  • 显示所有字符
print(s[:])

输出结果:hello
  • 显示前3个字符
print(s[:3])

运行结果为:hel
  • #字符串逆序输出
print(s[::-1])

运行结果为:olleh
  • 除了第一个字符以外,其他全部输出
print(s[1:])

运行结果为:ello
  • 重复
print( s * 10)

运行结果为:hellohellohellohellohellohellohellohellohellohello
  • 连接
print('hello' + ' ' + 'world')

运行结果为:hello world
  • 成员操作符
print('h' in s)
print('q' in s)
运行结果为:
True
False

练习:

num = input('Num:')

if num == num[::-1]:
    print('回文数')
else:
    print('不是')

  • 字符串判断
  • 判断字符串里面每个元素是否为某种类型
print('123'.isdigit())
print('123abc'.isdigit())
  • title:首字母大写,其余字母小写
print('Hello'.istitle())
print('HeLlo'.istitle())
print('hello'.upper())
print('hello'.isupper())
print('hElLo'.lower())
print('hElLo'.islower())
 print('hello123'.isalnum())
 print('123'.isalpha())
 print('abc'.isalpha())

print(isinstance(1,int))
print(isinstance('a',str))
print(isinstance(1,str))
  • 字符串去掉开头和结尾
 In [8]: s = '    hello   '
 In [9]: s.strip()
 Out[9]: 'hello'
 In [10]:
 In [10]: s.lstrip()
 Out[10]: 'hello   '
 In [11]: s.rstrip()
 Out[11]: '    hello'
 In [12]: s = '    \nhello   '
 In [13]: s.strip()
 Out[13]: 'hello'
 In [14]: s = '    \thello   '
 In [15]: s.strip()
Out[15]: 'hello'
In [16]: s = 'helloh'
 In [17]: s.strip('h')
Out[17]: 'ello'
In [18]: s.lstrip('he')
 Out[18]: 'lloh'

  • 字符串匹配开头和结尾
 filename = 'hello.log'
 if filename.endswith('.log'):
    print(filename)
 else:
  print('error')

url1 = 'file:///mnt'
url2 = 'ftp://172.25.254.250/pub'
url3 = 'http://172.25.254.250/index.html'

if url3.startswith('http://'):
    print('爬取该网页')
else:
    print('错误网页')
  • 字符串练习
变量名是否合法:
1.变量名只能由字母、数字、下划线组成
2.只能以字母或下划线开头

"""

#1.变量名第一个字符是否为字母或者下划线
#2.如果是,继续 --> 4
#3.如果不是,报错 , 退出
#4.依次判断除了第一个字符以外的其他字符
#5.判断是否为字母数字或者下划线

while True:
    s = input('变量名:')
    if s == 'exit':
        print('欢迎下次使用')
        break
    if s[0].isalpha() or s[0] == '_':
        for i in s[1:]:
            if not (i.isalnum() or i == '_'):
                print('%s变量名不合法' %s)
                break
        else:
            print('%s变量名合法' %s)
    else:
        print('%s变量名不合法' %s)

  • 字符串搜索和替换
s = 'hello world hello'

#find找到子串,并返回最小的索引
print(s.find('hello'))
print(s.find('world'))

#rfind找到子串,并返回最大的索引值
print(s.rfind('hello'))

#替换字符串中所有的‘hello’为‘westos’
print(s.replace('hello','westos'))

  • 字符串统计
print('hello'.count('l'))
print('hello'.count('ll'))
print(len('hello'))

  • 字符串的分离和连接
s = '172.25.254.250'
s1 = s.split('.')
print(s1)
print(s1[::-1])

date = '2019-03-17'
date1 = date.split('-')
print(date1)

print(''.join(date1))
print('/'.join(date1))

  • 练习

1.给定一个字符串来代表一个学生的出勤纪录,这个纪录仅包含以
下三个字符:
‘A’ : Absent,缺勤
‘L’ : Late,迟到
‘P’ : Present,到场
如果一个学生的出勤纪录中不超过一个’A’(缺勤)并且不超过两>个连续的’L’(迟到),
那么这个学生会被奖赏。
你需要根据这个学生的出勤纪录判断他是否会被奖赏。
示例 1:
输入: “PPALLP”
输出: True
示例 2:
输入: “PPALLL”
输出: False

s = input('输入考勤记录:')
if s.count('A') <= 1 and s.count('LLL') == 0:
    print('True')
else:
    print('False')

第二种写法:
print(s.count('A') <= 1 and s.count('LLL') == 0)

“”"

“”"
2.输入
hello xiao mi
输出
mi xiao hello
“”"

print(' '.join(input().split()[::-1]))

“”"
输入
They are students.
aeiou
输出
Thy r stdnts.
“”"

s1 = input('s1:')
s2 = input('s2:')

for i in s1:
    if i in s2:
        s1 = s1.replace(i,'')

print(s1)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值