python 内置函数或函数(争取日更)

本文介绍了Python中的rjust()函数用于字符串右对齐并填充,rstrip()移除字符串末尾字符,以及repr()函数的字符串表示形式。通过实例演示了如何在实际开发中使用这些方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Python rjust()方法


描述

Python rjust() 返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串。如果指定的长度小于字符串的长度则返回原字符串。

语法

rjust()方法语法:

str.rjust(width[, fillchar]).rjust(width[, fillchar])

参数

  • width -- 指定填充指定字符后中字符串的总长度.
  • fillchar -- 填充的字符,默认为空格。

返回值

返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串。如果指定的长度小于字符串的长度则返回原字符串

实例

以下实例展示了rjust()函数的使用方法:

#!/usr/bin/python

str = "this is string example....wow!!!";

print str.rjust(50, '0');

str = "this is string example....wow!!!";

print str.rjust(50, '0');

以上实例输出结果如下:

000000000000000000this is string example....wow!!! is string example....wow!!!
<span style="color:#666600">
</span>

python assert断言函数

python assert断言是声明布尔值必须为真的判定,如果发生异常就说明表达式为假。
可以理解assert断言语句为raise-if-not,用来测试表示式,其返回值为假,就会触发异常。

self.assertEqual(a,b,msg=msg)   #判断a与.b是否一致,msg类似备注,可以为空

self.assertNotEqual(a,b,msg=msg)  #判断a与b是否不一致

self.assertTrue(a,msg=none)    #判断a是否为True

self.assertFalse(b,msg=none)   #判断b是否为false

Python rstrip()方法

正在上传…重新上传取消 Python 字符串


描述

Python rstrip() 删除 string 字符串末尾的指定字符(默认为空格).

语法

rstrip()方法语法:

str.rstrip([chars]).rstrip([chars])

参数

  • chars -- 指定删除的字符(默认为空格)

返回值

返回删除 string 字符串末尾的指定字符后生成的新字符串。

实例

以下实例展示了rstrip()函数的使用方法:

#!/usr/bin/python

str = "     this is string example....wow!!!     ";
print str.rstrip();
str = "88888888this is string example....wow!!!8888888";
print str.rstrip('8');

str = "     this is string example....wow!!!     ";
print str.rstrip();
str = "88888888this is string example....wow!!!8888888";
print str.rstrip('8');

以上实例输出结果如下:

     this is string example....wow!!!
88888888this is string example....wow!!!this is string example....wow!!!
88888888this is string example....wow!!!

Python中的repr()函数

Python 有办法将任意值转为字符串:将它传入repr() 或str() 函数。

    函数str() 用于将值转化为适于人阅读的形式,而repr() 转化为供解释器读取的形式。

  在python的官方API中这样解释repr()函数:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值