Python字符串对齐和判断方法汇总!
Python字符串对齐和判断方法汇总!
作者:全栈若城
本教程将详细介绍Python中的字符串对齐方法以及字符串判断方法,这些方法在文本处理、格式化输出和字符串匹配中非常实用,无论你是Python初学者还是想要巩固基础知识的程序员,这篇教程都能帮助你全面理解这些操作,需要的朋友可以参考下
Python ljust()、rjust()和center()方法
1. ljust()方法:左对齐
ljust()方法用于将字符串左对齐,并使用指定字符(默认为空格)填充至指定长度。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
# 基本语法:str.ljust(width[, fillchar]) # width:字符串的总长度 # fillchar:填充字符(可选,默认为空格) # 示例1:使用默认空格填充 text = "Python" result = text.ljust( 10 ) print (f "[{result}]" ) # 输出:[Python ] # 示例2:使用自定义字符填充 result = text.ljust( 10 , '*' ) print (f "[{result}]" ) # 输出:[Python****] |
2. rjust()方法:右对齐
rjust()方法用于将字符串右对齐,并使用指定字符(默认为空格)填充至指定长度。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
# 基本语法:str.rjust(width[, fillchar]) # 示例1:使用默认空格填充 text = "Python" result = text.rjust( 10 ) print (f "[{result}]" ) # 输出:[ Python] # 示例2:使用自定义字符填充 result = text.rjust( 10 , '0' ) print (f "[{result}]" ) # 输出:[0000Python] # 示例3:数字格式化 price = "99" formatted_price = price.rjust( 6 , '0' ) print (formatted_price) # 输出:000099 |
3. center()方法:居中对齐
center()方法用于将字符串居中对齐,并使用指定字符(默认为空格)填充至指定长度。
1
2
3
4
5
6
7
8
9
10
11
12
|
# 基本语法:str.center(width[, fillchar]) # 示例1:使用默认空格填充 text = "Python" result = text.center( 10 ) print (f "[{result}]" ) # 输出:[ Python ] # 示例2:使用自定义字符填充 result = text.center( 10 , '-' ) print (f "[{result}]" ) # 输出:[--Python--] |
4. 实际应用场景
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
# 制作简单的文本表格 def print_table_row(item, price, width = 20 ): item_col = item.ljust(width) price_col = str (price).rjust( 8 ) print (f "{item_col}{price_col}" ) # 打印表头 print ( "商品清单" .center( 28 , '=' )) print_table_row( "商品" , "价格" ) print ( "-" * 28 ) # 打印数据 print_table_row( "苹果" , 5.5 ) print_table_row( "香蕉" , 3.8 ) print_table_row( "橙子" , 4.2 ) # 输出: # =========商品清单========= # 商品 价格 # ---------------------------- # 苹果 5.5 # 香蕉 3.8 # 橙子 4.2 |
Python startswith()和endswith()方法
1. startswith()方法:判断字符串开头
startswith()方法用于检查字符串是否以指定的前缀开始。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
# 基本语法:str.startswith(prefix[, start[, end]]) # prefix:要检查的前缀,可以是字符串或元组 # start:可选,开始检查的位置 # end:可选,结束检查的位置 # 示例1:基本用法 filename = "example.txt" print (filename.startswith( "ex" )) # 输出:True print (filename.startswith( "py" )) # 输出:False # 示例2:指定检查范围 text = "Hello, Python!" print (text.startswith( "Python" , 7 )) # 输出:True # 示例3:多个前缀(使用元组) filename = "document.pdf" print (filename.startswith(( "doc" , "txt" , "pdf" ))) # 输出:True |
2. endswith()方法:判断字符串结尾
endswith()方法用于检查字符串是否以指定的后缀结束。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
# 基本语法:str.endswith(suffix[, start[, end]]) # 示例1:基本用法 filename = "example.txt" print (filename.endswith( ".txt" )) # 输出:True print (filename.endswith( ".pdf" )) # 输出:False # 示例2:指定检查范围 text = "Hello, Python!" print (text.endswith( "Python" , 0 , 12 )) # 输出:True # 示例3:多个后缀(使用元组) filename = "document.pdf" print (filename.endswith(( ".doc" , ".txt" , ".pdf" ))) # 输出:True |
3. 实际应用场景
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# 示例1:文件类型检查 def is_image_file(filename): return filename.lower().endswith(( '.png' , '.jpg' , '.jpeg' , '.gif' )) # 测试文件类型 files = [ 'photo.jpg' , 'document.pdf' , 'image.png' , 'script.py' ] for file in files: if is_image_file( file ): print (f "{file} 是图片文件" ) else : print (f "{file} 不是图片文件" ) # 示例2:URL协议检查 def check_url_protocol(url): if url.startswith( 'https://' ): return "安全连接" elif url.startswith( 'http://' ): return "不安全连接" else : return "未知协议" # 测试URL urls = [ 'https://www.example.com' , 'http://www.example.com' , 'ftp://www.example.com' ] for url in urls: print (f "{url}: {check_url_protocol(url)}" ) |
总结
本教程详细介绍了Python中的字符串对齐方法(ljust、rjust和center)以及字符串判断方法(startswith和endswith):
- 字符串对齐方法:
- ljust():左对齐文本
- rjust():右对齐文本
- center():居中对齐文本
这些方法在格式化输出、创建文本表格等场景中非常有用。
- 字符串判断方法:
- startswith():检查字符串开头
- endswith():检查字符串结尾
这些方法在文件类型检查、URL验证等场景中经常使用。
掌握这些方法可以帮助你更好地处理文本数据,创建格式化输出,以及进行字符串匹配和验证。
学习资料见知识星球。
以上就是今天要分享的技巧,你学会了吗?若有什么问题,欢迎在下方留言。
快来试试吧,小琥 my21ke007。获取 1000个免费 Excel模板福利!
更多技巧, www.excelbook.cn
欢迎 加入 零售创新 知识星球,知识星球主要以数据分析、报告分享、数据工具讨论为主;
1、价值上万元的专业的PPT报告模板。
2、专业案例分析和解读笔记。
3、实用的Excel、Word、PPT技巧。
4、VIP讨论群,共享资源。
5、优惠的会员商品。
6、一次付费只需129元,即可下载本站文章涉及的文件和软件。
共有 0 条评论