python字符串的魔法

简介: print("hello,world.")
print("hello,world.")

判断输入是否为数字

test = "2"
v1 = test.isdecimal()    #这个用的最多,可以判断小数
v2 = test.isdigit()
v3 = test.isnumeric()    #支持中文判断
print(v1,v2,v3)
---
False  False True

判断是否存在不可显示的字符

# 如果存在\t \n等则为False
test = "sd\tfsdf"
v = test.isprintalbe()
print(v)
---
False

判断是否全为空格

test = " "
v = test.isspace()
print(v)
---
True

判断是否是标题

test = "Return True if all"
v = test.istitle
print(v)
---
False

转换为标题

v2 = test.title()
print(v2)
---
Return True If All

 将字符串中的每一个元素按指定分隔符进行拼接

# join会循环字符串,在每个字符后面加上指字的分隔符
test = "你是风儿我是沙"
t = ' '
v = t.join(test)
v = "_".join(test)
print(v)

设置宽度,并将内容居中

# 20 代指总长度
# * 空白未知填充,一个字符,可有可无
v = test.centre(20,"中")
print(v)


#### 

test = "alex"
v = test.ljust(20,"*")
print(v)

目录
相关文章
|
4天前
|
开发者 Python
Python中的f-string:高效字符串格式化的利器
Python中的f-string:高效字符串格式化的利器
154 99
|
7天前
|
Python
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
|
7天前
|
开发者 Python
Python f-strings:更优雅的字符串格式化技巧
Python f-strings:更优雅的字符串格式化技巧
|
7天前
|
开发者 Python
Python f-string:高效字符串格式化的艺术
Python f-string:高效字符串格式化的艺术
|
18天前
|
Python
使用Python f-strings实现更优雅的字符串格式化
使用Python f-strings实现更优雅的字符串格式化
|
1月前
|
Python
Python中的f-string:更简洁的字符串格式化
Python中的f-string:更简洁的字符串格式化
206 92
|
1月前
|
索引 Python
python 字符串的所有基础知识
python 字符串的所有基础知识
182 0
|
1月前
|
Python
Python字符串center()方法详解 - 实现字符串居中对齐的完整指南
Python的`center()`方法用于将字符串居中,并通过指定宽度和填充字符美化输出格式,常用于文本对齐、标题及表格设计。
|
2月前
|
PHP Python
Python format()函数高级字符串格式化详解
在 Python 中,字符串格式化是一个重要的主题,format() 函数作为一种灵活且强大的字符串格式化方法,被广泛应用。format() 函数不仅能实现基本的插入变量,还支持更多高级的格式化功能,包括数字格式、对齐、填充、日期时间格式、嵌套字段等。 今天我们将深入解析 format() 函数的高级用法,帮助你在实际编程中更高效地处理字符串格式化。
258 0

推荐镜像

更多