python-str字符串

1.字符串类型的判断

# 判断字符串里面的每个元素是否为什么类型

#可以加/alnum/alpha/digit/lower/space/title/upper

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

print '123'.isdigit()

print '123abc'.isdigit()

# title:标题 判断某个字符串是否为标题(第一个首字母大写,其余字母小写)

print 'Hello'.istitle()

print 'HeLlo'.istitle()

2.找出字符串是否以XXX结尾

s = 'hello.jpg'

print s.endswith('.png')

url1 = 'http://www.baidu.com'

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

3.字符串的分离和连接

s = '172.25.254.250'

s1 = s.split('.')    #”.”k可用其他分隔符来替换

print s1

连接

print ''.join(date1)

print '/'.join('hello')

4.字符串的定义方式

a = "hello"

b = 'westos'

c = "what's up"

d = """

    用户管理

1.添加用户

2.删除用户

3.显示用户

"""

5.字符串的搜索和替换

s = 'hello world'

print len(s)

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

print s.find('hello')

print s.find('world')

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

6.字符串的特性

# 索引: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

7.字符串的统计

print 'helloooo'.count('o')

 

猜你喜欢

转载自blog.csdn.net/weixin_40460156/article/details/82154325
今日推荐