Python下的字符串(定义、特性、判断等)

116 篇文章 0 订阅
114 篇文章 0 订阅

1.字符串的定义

"""
a = 'westos'
b = "what's"
c = """
    用户管理系统
    1.添加用户
    2.删除用户
    3.显示用户
    ....

"""
print(c)

在这里插入图片描述
在这里插入图片描述

2.字符串的特性①(索引、链接等)

s = 'hello' # 索引:0 1 2 3 4 索引从0开始
print(s[0])
print(s[1])

# 拿出最后 一个字符

print(s[4])
print(s[-1])

# s[start:stop:step] 从satrt开始到end -1结束

# 步长为step

print(s[0:3])
print(s[0:4:2])

# 显示所有的字符

print(s[:])

# 显示前3个字符

print(s[:3])

# 字符串的反转

print(s[::-1])

# 除了第一个字符之外的其他全部字符

print(s[1:])

# 重复

print(s * 10)

# 连接

print('hello ' + 'python')

# 成员操作符

print('he' in s)
print('aa' in s)
print('he' not in s)

在这里插入图片描述
在这里插入图片描述

3.字符串判断大小写

'hello'.istitle()
False
'hello'.isupper()
False
'Hello'.isupper()
False
'HELLO'.isupper()
True
'hello'.islower()
True
'Hello'.islower()
False

在这里插入图片描述

4.判断字符串的结尾和开头

filename = 'hello.log'
if filename.endswith('.log'):
    print(filename)
else:
    print('error.file')

url = 'https://172.25.254.250/index.html'
if url.startswith('http://'):
    print('爬取内容~~')
else:
    print('不能爬取~~')

在这里插入图片描述

5.字符串的特性②

注意:去除左右两边的空格,空格为广义的空格 包括:\t \n

s = '       hello'
s
'\thello'
s = '        hello'
s
'        hello'
s.lstrip()
'hello'
s = '        hello      '
s
'        hello      '
s.lstrip()
'hello      '
s.rstrip()
'        hello'
s.strip()
'hello'
s = '       hello'
s
'\thello'
s.strip()
'hello'
s = '       hello\n'
s
'       hello\n'
s.strip()
'hello'
s = 'helloh'
s.strip('h')
'ello'
s.lstrip('h')
'elloh'

在这里插入图片描述
在这里插入图片描述

6.字符串的判断

# 只要有一个元素不满足,就返回False

print('weeeqwe32131'.isdigit())
print('42131321fsas'.isalpha())
print('weewqeq323213'.isalnum())

在这里插入图片描述

7.字符串的对齐

print('学生管理系统'.center(30))
print('学生管理系统'.center(30,'@'))
print('学生管理系统'.center(30,'&'))
print('学生管理系统'.ljust(30,'#'))
print('学生管理系统'.rjust(30,'#'))

在这里插入图片描述

8.字符串的替换

s = 'hello world hello'

# find 找到子字符串,并返回最小的索引

print(s.find('hello'))
print(s.find('world'))
print(s.rfind('hello'))

# 替换字符串中的hello为redhat

print(s.replace('hello','redhat'))

在这里插入图片描述

9.字符串的分离和连接

s = '172.25.254.250'
s1 = s.split('.')
print(s1)
print(s1[::-1])

date = '2019-8-28'
date1 = date.split('-')
print(date1)

# 连接 通过指定的连接符号 连接每个字符

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

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值