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 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()函数: