字符串处理

一、字符串的拼接

字符串或串(String)是由数字、字母、下划线组成的一串字符。在 Python 中,字符串用单引号或者双引号括起来。

Python 中使用“ + ”来合并两个字符串,这种合并字符串的方法叫做拼接。其基本语法如下:

result_string = source_string1 + source_string2

其中:

  • source_string1:待合并的第一个字符串;

  • source_string2:待合并的第二个字符串;

  • result_string:合并后的字符串。

 注意:如果需要,在两个字符串之间可以增加相应的空格,具体见下面的例子。例如,将姓氏和名字拼接成全名:

# coding=utf-8
# 将姓氏和名字分别保存在两个变量中
first_name = 'Zhang'
last_name = 'san'
# 将姓氏和名字拼接,将结果存储在full_name变量中
full_name = first_name + " " + last_name
print(full_name)

#输出结果
#Zhang san
例:实现如下功能:

将存放姓氏的字符串变量和存放名字的字符串变量拼接起来,中间用一个空格隔开,并将结果存储在full_name变量中;

打印输出full_name变量。
# coding=utf-8

# 存放姓氏和名字的变量
first_name = input()
last_name = input()

# 请在下面添加字符串拼接的代码,完成相应功能
full_name = first_name + " " + last_name
print(full_name)

二、字符转换

1.字符串长度获取(len()

Python 提供了len()函数来计算,并返回字符串的长度,即字符串中单个元素的个数。其基本语法如下:

length = len(target_string)

其中:

  • target_string: 目标字符串变量;

  • length: 保存字符串长度的变量;

  • len: 获取字符串长度的语法关键词。

# coding=utf-8
# 创建一个字符串变量,获取其长度并打印出来
color = 'It is red'
length = len(color)
print (length)
# 直接在len函数中引入字符串内容获得其长度,然后打印出来
print(len('This is a circle!'))

#输出结果
9
17

 注意: 从输出结果可以看到,空格也占一个字符元素的位置。

3.大小写转换(upper(大写)、lower(小写)、title(首字母大写)

Python 提供了upper()lower()方法,来对字符串进行大小写转换。

upper()会将字符串中的所有字符都转换为大写

lower()则将所有字符转换为小写

title()方法,将字符串所有单词的首字母变成大写,而其他字母依然小写。

各个方法的具体语法如下:

  1. # 将源字符串转换为大写并存入upper_string变量
  2. upper_string = source_string.upper()
  3. # 将源字符串转换为小写并存入lower_string变量
  4. lower_string = source_string.lower()
  5. # 将源字符串每个词首字母转换为大写并存入title_string变量
  6. title_string = source_string.title()

 其中,source_string为待处理的源字符串。具体使用示例如下:

# coding=utf-8
# 创建一个字符串say_hello
say_hello = 'Dear my Daughter'
# 使用upper()方法对say_hello字符串进行处理
upper_say_hello = say_hello.upper()
# 使用lower()方法对say_hello字符串进行处理
lower_say_hello = say_hello.lower()
# 使用title()方法对say_hello字符串进行处理
title_say_hello = say_hello.title()
# 打印输出四个字符串
print (say_hello+"\n")
print (upper_say_hello+"\n")
print (lower_say_hello+"\n")
print (title_say_hello+"\n")

#输出结果
Dear my Daughter
DEAR MY DAUGHTER
dear my daughter
Dear My Daughter

 注意: 由上述打印结果可以看出,上述方法的调用,并不会对原始的 say_hello字符串产生影响,转换后的字符串会存入新的变量中。

4.去除字符串首尾空格(strip()

Python 提供了strip()方法,可以去除字符串两侧(不包含内部)全部的空格。使用该方法,也可以通过指定参数,去除两侧指定的特定字符。

注意:在指定参数时,如果参数是多个字符,则该方法会将多个字符逐个去比对,进行删除(区分大小写),直到首尾两侧没有匹配的字符为止。但是,该方法对字符串中间的字符没有影响。

其基本语法如下:

  1. strip_string1 = source_string.strip()
  2. strip_string2 = source_string.strip(target_char)

其中:

  • source_string:待处理的源字符串;

  • strip_string1strip_string2:处理后的字符串;

  • target_char:需要从源字符串首尾去除的特定字符。

具体使用示例如下:

# coding = utf-8
# 创建一个字符串hello_world
hello_world = '  **The world ** is big!*    '
# 利用strip()方法处理hello_world字符串
blank_hello_world = hello_world.strip()
char_hello_world = hello_world.strip('TH *')
# 打印输出转换后的字符串
print(blank_hello_world)
print(char_hello_world)

#输出结果
**The world ** is big!*
he world ** is big!

 输出结果分析:

  • 从第一行打印结果可以看到,strip()方法去除了源字符串首尾的所有空格,但是并没有去除字符串中间的空格;

  • 从第二行打印结构可以看出,strip()方法将源字符串首尾所有空格、* 以及字符T去掉了,而源字符串中头部的h因为是小写并没有去除。

例:实现给定字符串的转换功能,具体要求如下:

step1 :将输入的源字符串source_string首尾的空格删除;

step2 :将 step1 处理后的字符串的所有单词的首字母变为大写,并打印输出;

step3 :将 step2 转换后的字符串的长度打印输出。

# coding=utf-8

# 获取待处理的源字符串
source_string = input()

# 请在下面添加字符串转换的代码
strip_string = source_string.strip(' ')
title_strip_string = strip_string.title()
print(title_strip_string+"\n")
#/n为换行
length = len(title_strip_string)
print(length)

 三、字符串查找与替换

1.字符串查找(find()

Python 提供了内置的字符串查找方法find(),利用该方法可以在一个较长的字符串中查找子字符串。如果该字符串中,有一个或者多个子字符串,则该方法返回第一个子串所在位置的最左端索引,若没有找到符合条件的子串,则返回-1find()方法的基本使用语法如下:

source_string.find(sub_string)

其中:

  • source_string:源字符串;

  • sub_string:待查的目标子字符串;

  • find:字符串查找方法的语法关键字。

例如,在一个字符串中,查找两个单词的位置:
# coding=utf-8
# 创建一个字符串
source_string = 'The past is gone and static'
# 查看"past"在source_string字符串中的位置
print(source_string.find('past'))
# 查看"love"在source_string字符串中的位置
print(source_string.find('love'))

#输出结果
4
-1

 2.字符串替换(replace()

Python 提供了replace()方法,用以替换给定字符串中的子串。其基本使用语法如下:

source_string.replace(old_string, new_string)

 其中:

  • source_string:待处理的源字符串;

  • old_string:被替换的旧字符串;

  • new_string:替换的新字符串;

  • replace:字符串替换方法的语法关键词。

例如,在如下字符串中,用small子串替换big子串:
# coding = utf-8
# 创建一个字符串circle
source_string = 'The world is big'
# 利用replace()方法用子串"small"代替子串"big"
print(source_string.replace('big','small'))

#输出结果
The world is small

3.字符串分割(split()

Python 提供了split()方法实现字符串分割。该方法根据提供的分隔符,将一个字符串分割为字符列表,如果不提供分隔符,则程序会默认把空格(制表、换行等)作为分隔符。其基本使用语法如下:

source_string.split(separator)

其中:

  • source_string:待处理的源字符串;

  • separator:分隔符;

  • split:字符串分割方法的关键词。 

例如,用+、/还有空格作为分隔符,分割字符串:
# coding = utf-8
# 待处理字符串source_string
source_string = '1+2+3+4+5'
# 利用split()方法,按照`+`和`/`对source_string字符串进行分割
print(source_string.split('+'))
print(source_string.split('/'))

#输出结果
['1', '2', '3', '4', '5']
['1+2+3+4+5']
例:实现如下功能:

step1 :查找输入字符串source_string中,是否存在day这个子字符串,并打印输出查找结果;

step2 :对输入字符串source_string执行字符替换操作,将其中所有的 day替换为time,并打印输出替换后的字符串;

step3 :对 step2 进行替换操作后的新字符串,按照空格进行分割,并打印输出分割后的字符列表。

# coding = utf-8
source_string = input()

# 请在下面添加代码
source_string.find('day')
print(source_string.find('day'))
source_string.replace('day','time')
print(source_string.replace('day','time'))
method_string = source_string.replace('day','time')
method_string.split(' ')
print(method_string.split(' '))

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值