- 回文数
- 更换大小写
- capitalize()
- casefold()
- title()
- swapcase()
- upper()
- lower()
- 左中右对齐
- center(a,b)
- ljust(a,b)
- rjust(a,b)
- zfill(a)
array = "12321"
print("是回文数") if array==array[::-1] else print("不是回文数")
更换大小写
| 函数 | 功能 |
|---|---|
| capitalize() | 第一个字母大写其他小写 |
| casefold() | 全部小写 |
| title() | 首字母大小其他小写 |
| swapcase() | 翻转大小写 |
| upper() | 所有字母大写 |
| lower() | 所有字母小写 |
x = "hello world" print(x.capitalize())
casefold()
x = "HelLo World" print(x.casefold())
title()
x = "hello world" print(x.title())
swapcase()
x = "Hello WoRld" print(x.swapcase())
upper()
x = "hello world" print(x.upper())
lower()
x = "HELLO World" print(x.lower())
左中右对齐
| 函数 | 功能 |
|---|---|
| center() | 居中 |
| ljust() | 左对齐 |
| rjust() | 右对齐 |
| zfill() | 右对齐,左填充0 |
x.center(20) 的含义为:包含x的字符串长度在内,一共20个字符,将x中内容 hello world 居中处理。
若x的长度超过 x.center(a) 中a的长度,则 center函数无效。
x = "hello world" print(x.center(20))
x.center(20,“-”) 的含义为:包含x的字符串长度内,一共20个字符,将 hello world 居中处理,且空白处用字符 - 填充。
x = "hello world" print(x.center(20,"-"))
ljust(a,b)
x.ljust(20) 的含义为:包含x的字符串长度在内,一共20个字符,将x中内容 hello world 居中处理。
若x的长度超过 x.ljust(a) 中a的长度,则 ljust函数无效。
x = "hello world" print(x.ljust(20))
同样,ljust可以更改填充值
x = "hello world" print(x.ljust(20,"-"))
rjust(a,b)
与ljust类比,这里不多赘婿
x = "hello world" print(x.rjust(20))
x = "hello world" print(x.rjust(20,"-"))
zfill(a)
zfill(a)可以类比于x.rjust(a,“0”)
x = "hello world" print(x.zfill(20)) print(x.rjust(20,"0"))



