python 常见字符串处理

目录

 

一、分解字符串

 ①、在文本文件中存储数据时,常见的方法就是将各个项用逗号分隔,将出现逗号的地方分解字符串。

②、 多个字符作为分解标记

 ③、没有指定任何分解标记,它会按空白符(whit- espace)分解字符串

 二、联接字符串

①、 + 把2个字符串连接起来,拼接

②、join() 函数:指出把哪些字符串联接起来

三、搜索字符串

①、startswith() 函数:指出一个字符串是否以某个字符或某几个字符开头

②、 endswith()函数:指出一个字符串是否以某个字符或某几个字符结尾

③、在字符串中搜索:in 和 index()

四、删除字符串的一部分

五、改变大小写


一、分解字符串

把一个长字符串分解成多个小字符串。

string.split()

 ①、在文本文件中存储数据时,常见的方法就是将各个项用逗号分隔,将出现逗号的地方分解字符串。

name_string = 'xiaoming, hong, xiaohua, meihua, cherry'
names = name_string.split(',')
print(names)

for name in names:
    print(name)

输出:
['xiaoming', ' hong', ' xiaohua', ' meihua', ' cherry']
xiaoming
 hong
 xiaohua
 meihua
 cherry

 

②、 多个字符作为分解标记

names = name_string.split(', meihua')
print(names)

for name in names:
    print(name)

输出:
['xiaoming, hong, xiaohua', ', cherry']
xiaoming, hong, xiaohua
, cherry

 ③、没有指定任何分解标记,它会按空白符(whit- espace)分解字符串

names = name_string.split()
print(names)
for name in names:
    print(name)

输出:
['xiaoming,', 'hong,', 'xiaohua,', 'meihua,', 'cherry']
xiaoming,
hong,
xiaohua,
meihua,
cherry

 二、联接字符串

①、 + 把2个字符串连接起来,拼接

print('dog' + 'sun')

输出:
dogsun

②、join() 函数:指出把哪些字符串联接起来

word =['my', 'name', 'is', 'chreey']
print(word)
long_string = ' '.join(word)
print(long_string)

long = 'hello  '.join(word)
print(long)


输出:
['my', 'name', 'is', 'chreey']
my name is chreey
myhello  namehello  ishello  chreey

三、搜索字符串

①、startswith() 函数:指出一个字符串是否以某个字符或某几个字符开头

name = 'frankenstein'
name.startswith('F')
name.startswith('f')
name.startswith('frank')
name.startswith('flop')

输出:
>>> name.startswith('F')
False
>>> name.startswith('f')
True
>>> name.startswith('frank')
True
>>> name.startswith('flop')
False

若想知道查找的字符串,是在哪个位置:

lines = ['cake', '2 eggs', 'sode', 'instructions','mix']
i = 0
while not lines[i].startswith('instructions'):
    i = i + 1

print(i)

输出:
3

②、 endswith()函数指出一个字符串是否以某个字符或某几个字符结尾

name = 'frankenstein'

name.endswith('n')
name.endswith('stein')

输出:
>>> name.endswith('n')

True
>>> name.endswith('stein')
True
>>> name.endswith('soop')
False

③、在字符串中搜索:in index()

in: 检查某个元素是否在列表中。

city = ['657 Maple Lane', '47 B Syreet', '95 Drive']

if '657 Maple Lane' in city:
    print('include in')

输出:
include in

in 关键字只能指出子串是不是位于检查的字符串中的某个位置,但没有告诉到底在什么位置。要得到这个位置,需要使用 index() 方法。

city = ['657 Maple Lane', '47 B Syreet', '95 Drive']

if ' Maple' in city[0]:
    print('include in')
    pos = city[0].index('Maple')
    print('found  at', pos)

输出:
include in
found  at 4

四、删除字符串的一部分

 strip():剔除末尾部分,如换行符或一些多余空格

name = 'afternoon ok?'
short_name = name.strip('ok?')
print(short_name)

name = 'afternoon ok?               '
short_name = name.strip()
print(short_name)

输出:
afternoon 
afternoon ok?

五、改变大小写

把字符串从大写转换为小写或从小写转换为大写。

str1 ='ABCDEFG'
str2 ='hijklmn'

strl1 = str1.lower()
print(strl1)
stru1 = str2.upper()
print(stru1)

输出:
abcdefg
HIJKLMN

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值