以20字符宽居中输出python字符串_Python字符串

字符串方法

>>> for i in dir(str):print(i)

capitalize 将字符串的第一个字符转换为大写

casefold 转换字符为小写,比lower()更强

center返回宽度 width ,字符串居中,两端填充fillchar的字符串

count返回指定字符串在字符串里出现的次数

encode 以指定的编码格式编码字符串

endswith判断字符串是否以指定后缀结尾

expandtabs把字符串中的 tab 符号\t转为空格

find检测字符串中是否包含子字符串,包含则返回sub的index,不包含返回-1

format 格式化字符串

format_map 格式化字符串

index 检测字符串中是否包含子字符串,类似find,但是不包含会触发异常

isalnum判断字符串至少有一个字符并且所有字符都是字母或数字

isalpha判断字符串至少有一个字符并且所有字符都是字母或中文

isdecimal 判断字符串至少有一个字符并且所有字符都是unicode数字和全角数字

isdigit 判断字符串至少有一个字符并且所有字符都是半角数字、全角数字、字节数字

isidentifier用于判断字符串是否是有效的 Python 标识符

islower判断字符串中区分大小写的字符是否只存在小写字符

isnumeric 判断字符串至少有一个字符并且所有字符都是半角数字、全角数字、中文数字

isprintable 判断字符串中所有字符是否都是可打印字符

isspace判断字符串中是否只包含空白字符

istitle判断字符串是否是标题化的

isupper判断字符串中区分大小写的字符是否只存在大写字符

join将序列中的元素以指定的字符连接生成一个新的字符串

ljust返回长度为 width 的字符串,原字符串左对齐,后面填充fillchar

lower转换字符串中所有大写字符为小写

lstrip去掉字符串头的空白字符

maketrans创建字符映射的转换表,给translate用

partition返回由分隔符前,分隔符,分隔符后组成的元组

replace 替换字符串

rfind类似于 find()函数,不过是从右边开始查找

rindex类似于 index(),不过是从右边开始.

rjust 返回长度为 width 的字符串,原字符串右对齐,前面填充fillchar

rpartition类似partition,从右往左

rsplit 去掉字符串尾的空白字符

rstrip 去掉字符串尾的空白字符

split 按照给定的分隔符将字符串分隔为列表

splitlines 返回字符串中的行列表

startswith判断字符串是否以指定字符串开始

strip 去掉字符串头和尾的空白字符

swapcase将字符串中大写转换为小写,小写转换为大写

title 将字符串标题化

translate根据转换表转换字符串

upper转换字符串中的小写字母为大写

zfill返回长度为 width 的字符串,原字符串右对齐,前面填充0

>>>

capitalize将字符串的第一个字符转换为大写

语法:

>>> help(str.capitalize)

Help on method_descriptor:

capitalize(...)

S.capitalize() -> str

Return a capitalized version of S, i.e. make the first character

have upper case and the rest lower case.

示例:

>>> s = 'hello world'

>>> s.capitalize()

'Hello world'

>>>

center/ljust/rjust/zfill/format/format_map格式化字符串

center 返回宽度 width 的字符串,原字符串居中,两端填充fillchar的字符串

fillchar默认为空格

语法:

>>> help(str.center)

Help on method_descriptor:

center(...)

S.center(width[, fillchar]) -> str

Return S centered in a string of length width. Padding is

done using the specified fill character (default is a space)

示例:

>>> s = 'hello world'

>>> s.center(30,'*')

'*********hello world**********'

ljust 返回长度为 width 的字符串,原字符串左对齐,后面填充fillchar

返回一个原字符串左对齐,并使用 fillchar 填充至长度 width 的新字符串,fillchar 默认为空格。

语法:

>>> help(str.ljust)

Help on method_descriptor:

ljust(...)

S.ljust(width[, fillchar]) -> str

Return S left-justified in a Unicode string of length width. Padding is

done using the specified fill character (default is a space).

示例:

>>> s = 'ab12'

>>> s.ljust(8,'*')

'ab12****'

rjust返回长度为 width 的字符串,原字符串右对齐,前面填充fillchar

语法:

>>> help(str.rjust)

Help on method_descriptor:

rjust(...)

S.rjust(width[, fillchar]) -> str

Return S right-justified in a string of length width. Padding is

done using the specified fill character (default is a space).

示例:

>>> s = 'ab12'

>>> s.rjust(8,'*')

'****ab12'

zfill 返回长度为 width 的字符串,原字符串右对齐,前面填充0

语法:

>>> help(str.zfill)

Help on method_descriptor:

zfill(...)

S.zfill(width) -> str

Pad a numeric string S with zeros on the left, to fill a field

of the specified width. The string S is never truncated.

示例:

>>> 'Ab12'.zfill()    #必须提供长度,不然报错

Traceback (most recent call last):

File "", line 1, in

'Ab12'.zfill()

TypeError: zfill() takes exactly 1 argument (0 given)

>>> 'Ab12'.zfill(8)

'0000Ab12'

format 格式化字符串

Python2.6 开始,新增了一种格式化字符串的函数 str.format(),它增强了字符串格式化的功能。

基本语法是通过 {} 和 : 来代替以前的 % 。

format 函数可以接受不限个参数,位置可以不按顺序。

语法:

>>> help(str.format)

Help on method_descriptor:

format(...)

S.format(*args, **kwargs) -> str

Return a formatted version of S, using substitutions from args and kwargs.

The substitutions are identified by braces ('{' and '}').

示例1:按位置格式化

>>> "{} {}".format("hello", "world")

'hello world'

>>> "{0} {1}".format("hello", "world")

'hello world'

>>> "{1} {0} {1}".format("hello", "world")

'world hello world'

示例2:按变量名格式化

>>> 'my name is {name},{age} years old'.format(name = 'bob',age = 18)

'my name is bob,18 years old'

>> D = {'name':'hh','age':18}

>>> 'my name is {name},{age} years old'.format(**D) #通过字典提供参数

'my name is hh,18 years old'

>>> 'my name is {0[0]},{0[1]} years old'.format(L) #通过列表提供参数

'my name is hh,18 years old'

format_map 类似format,参数是一个字典

语法:

>>> help(str.format_map)

Help on method_descriptor:

format_map(...)

S.format_map(mapping) -> str

Return a formatted version of S, using substitutions from mapping.

The substitutions are identified by braces ('{' and '}').

示例:

>>> 'my name is {name},{age} years old'.format_map({'name':'bob','age':18})

'my name is bob,18 years old'

count 返回指定字符串在字符串里出现的次数

在str[start:end]中查找sub出现的次数

语法:

>>> help(str.count)

Help on method_descriptor:

count(...)

S.count(sub[, start[, end]]) -> int

Return the number of non-overlapping occurrences of substring sub in

string S[start:end]. Optional arguments start and end are

interpreted as in slice notation.

示例:

>>> s = 'hello world'

>>> s.count('o')

2

>>> s.count('l

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值