python string_Python String center()

python string

Python string center() function returns a centered string of specified size. We can also specify the character to use for padding, the default is whitespace.

Python字符串center()函数返回指定大小的居中字符串。 我们还可以指定用于填充的字符,默认为空白。

Python String center() (Python String center())

The function syntax is:

函数语法为:

str.center(width[, fillchar])

Let’s look at some examples of using string center() method.

让我们看一些使用字符串center()方法的示例。

s = 'Hello'

s1 = s.center(20)
print(f'***{s1}***')

Output: *** Hello ***

输出: *** Hello ***

Note that my source string size is 5 and the new string size is 20. So both side paddings can’t be of equal size, here 7 whitespace characters are padded before the source string and 8 whitespace characters are padded after the source string.

请注意,我的源字符串大小为5,新字符串大小为20。因此,两个边距的大小不能相等,这里在源字符串之前填充7个空格字符,在源字符串之后填充8个空格字符。

NOTE: If you are not familiar with f-prefixed string formatting, it’s a new formatting method introduced in Python 3.6. You can read more about it in python f-strings article.

注意 :如果您不熟悉带f前缀的字符串格式,则它是Python 3.6中引入的一种新的格式化方法。 您可以在python f-strings文章中阅读有关它的更多信息。

What will happen if the string size is more than the specified width? Let’s find it out with a simple example.

如果字符串大小大于指定的宽度会怎样? 让我们用一个简单的例子找出来。

>>> s = 'Hello World'
>>> s.center(6)
'Hello World'
>>> s.center(12)
'Hello World '
>>> s.center(11)
'Hello World'
>>>

So if the string size is more than the specified width, the original string is returned without any padding or removing any characters.

因此,如果字符串大小大于指定的宽度,则将返回原始字符串,而不会进行任何填充或删除任何字符。

带有fillchar的Python String center() (Python String center() with fillchar)

s = 'Hello'

s1 = s.center(20, '#')
print(f'***{s1}***')

s1 = s.center(20, 'ç')
print(f'***{s1}***')

Before we conclude this post, let’s look at some error scenarios.

在结束本文之前,让我们看一些错误情况。

s.center('#')

Output: TypeError: 'str' object cannot be interpreted as an integer

输出: TypeError: 'str' object cannot be interpreted as an integer

The error occurred because we didn’t provide the length of the output string.

发生错误是因为我们没有提供输出字符串的长度。

s1 = s.center()

Output: TypeError: center() takes at least 1 argument (0 given)

输出: TypeError: center() takes at least 1 argument (0 given)

s.center(20, '#$')

Output: TypeError: The fill character must be exactly one character long

输出: TypeError: The fill character must be exactly one character long

That’s all for python string center() method. We can use it to create a new string of specific size with source string centered. It’s useful in creating a string to display purpose in UI or in HTML reports.

python字符串center()方法就这些了。 我们可以使用它来创建一个以源字符串为中心的特定大小的新字符串。 在创建字符串以在UI或HTML报表中显示用途时很有用。

GitHub Repository. GitHub存储库中检出完整的python脚本和更多Python示例。

Reference: API Documentation

参考: API文档

翻译自: https://www.journaldev.com/23919/python-string-center

python string

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值