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()