python - 字符串的内建函数

# -*- coding:utf-8 -*-

'''
@project: jiaxy
@author: Jimmy
@file: study_3_str_内建函数.py
@ide: PyCharm Community Edition
@time: 2018-11-01 15:48
@blog: https://www.cnblogs.com/gotesting/

'''

# 字符串的内建函数



# 1. 查找某个字符
# 字符串.find(指定的字符或者子字符串)
# 若没有找到,则返回-1
# 若找到,则返回索引

s = '明天,你好!'
print(s.find('@'))
print(s.find('好'))
print(s.find('你好'))


# 2. 字符串的替换
# 字符串.replace(目标,最终替换成什么)

l = '举杯望明月'
new_l = l.replace('望','看')
print(new_l)


# 3. 字符串的切割
# 字符串.split()
# 返回的数据类型 是 列表

t = 'hi@Jimmy'
t1 = t.split('@')
print('切割后的结果值{}'.format(t1))


# 4. 去除指定字符
# 字符串.strip()
# 去掉头和尾 指定的字符

m = 'I love Python 6 ! 666'
m1 = m.strip('6')
print('去掉6后的结果值{}'.format(m1))


# 5.变换大小写
# 字符串.upper() 变成大写
# 字符串.lower() 变成小写
a = 'hello'
b = 'WORLD'
c = 'IloveYOU'
a1 = a.upper()
b1 = b.lower()
c1 = c.upper()
c2 = c.lower()
print(a1,b1,c1,c2)


# 6.首字母大写
# 字符串.capitalize()
print(c.capitalize())

# 7. 统计字符串中字符的个数
# 字符串.count(指定字符)
print(c.count('o'))

# 8. 居中
# 字符串.center()

# 9. 返回字符所在索引
# 字符串.index(指定字符)
print(c.index('o'))

 

 


posted @ 2018-11-01 16:46  JiaxyGogogo  阅读(224)  评论(0编辑  收藏  举报