PYTHON---str_字符串

1.字符串的定义

a = "hello"
b = 'world'

print a
print b

2.字符串特性

## 索引:0,1,2,3,4 索引值是从0开始
s = 'hello'
print s[0]
print s[1]

## 切片
print s[0:3]  # 切片的规则:s[start:end:step] 从start开始到end-1结束,步长:step
print s[0:4:2]
# 显示所有字符
print s[:]
# 显示前3个字符
print s[:3]
# 对字符串倒叙输出
print s[::-1]
# 除了第一个字符以外,其他全部显示
print s[1:]

## 重复
print s * 10

## 连接
print 'hello ' + 'world'

## 成员操作符
print 'q' in s
print 'he' in s
print 'aa' in s

3.字符串的搜索和替换

s = 'hello world'

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

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

4.字符串的分离和连接

# split对于字符串进行分离,分割符为'.'
s = '172.25.254.250'
s1 = s.split('.')
print s1

date = '2018-8-27'
date1 = date.split('-')
print date1

# 连接
print ''.join(date1)
print '/'.join(date1)
print '/'.join('hello')

5.字符串开头和结尾匹配

s = 'hello.jpg'
# 找出字符串是否以XXX结尾
print s.endswith('.png')

url1 = 'http://www.baidu.com'
url2 = 'file:///mnt'

print url1.startswith('http://')
print url2.startswith('f')

6.字符串判断是否大小写或数字

# 判断字符串里面的每个元素是否为什么类型
# 一旦有一个元素不满足,就返回False
print '123'.isdigit()
print '123abc'.isdigit()

# title:标题 判断某个字符串是否为标题(第一个首字母大写,其余字母小写)
print 'Hello'.istitle()
print 'HeLlo'.istitle()
print 'hello'.upper()
print 'hello'.islower()
print 'HELLO'.lower()
print 'HELLO'.isupper()
print 'hello'.isalnum()
print '123'.isalpha()
print 'qqq'.isalpha()

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值