python笔记之字符串处理:
  ZTR47FrQxTR9 2023年11月02日 28 0


python笔记:

格式字符串中,不同占位符的含义:
%s: 作为字符串
%d: 作为有符号十进制整数
%u: 作为无符号十进制整数
%o: 作为无符号八进制整数
%x: 作为无符号十六进制整数,a~f采用小写形式
%X: 作为无符号十六进制整数,A~F采用大写形式
%f: 作为浮点数
%e,%E: 作为浮点数,使用科学计数法
%g,%G: 作为浮点数,使用最低有效数位
字符串转换
input函数接收的是用户输入的字符串,此时还不能作为整数或者小数进行数学运算,需要使用函数将字符串转换成想要的类型。
转换成整数,使用int()函数:num1 = int(str)
转换成小数,使用float()函数:f1 = float(str)
str = input()
num1 = int(str)
f1 = float(str)
print("整数%d,小数%f" % (num1,f1))
如果输入10,得到的输出是:整数10,小数10.000000。
Python 提供了len()函数来计算,并返回字符串的长度,即字符串中单个元素的个数。
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

大小写转换

Python 提供了upper()和lower()方法,来对字符串进行大小写转换。其中,upper()会将字符串中的所有字符都转换为大写,lower()则将所有字符转换为小写。除此之外,Python 还贴心的提供了title()方法,将字符串所有单词的首字母变成大写,而其他字母依然小写。各个方法的具体语法如下:
将源字符串转换为大写并存入upper_string变量
upper_string = source_string.upper()

# 将源字符串转换为小写并存入lower_string变量
lower_string = source_string.lower()

# 将源字符串每个词首字母转换为大写并存入title_string变量
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

去除字符串首尾空格

Python 提供了strip()方法,可以去除字符串两侧(不包含内部)全部的空格。使用该方法,也可以通过指定参数,去除两侧指定的特定字符。
注意:在指定参数时,如果参数是多个字符,则该方法会将多个字符逐个去比对,进行删除(区分大小写),直到首尾两侧没有匹配的字符为止。但是,该方法对字符串中间的字符没有影响。
其基本语法如下:
strip_string1 = source_string.strip()
string_strip2 = source_string.strip(target_char)
其中:
source_string:待处理的源字符串;
strip_string1和strip_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因为是小写并没有去除。

字符串查找

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

字符串替换

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

字符串分割

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']


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  4kp26FQmv7NK   2023年11月02日   32   0   0 项目管理Python信息系统
  PVcilKyJJTzb   2023年11月02日   44   0   0 3gPython依赖关系
ZTR47FrQxTR9