欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

笨办法学python 习题6:字符串(string)和文本

程序员文章站 2022-03-04 14:17:27
...

熟悉字符串和文本

1、%r 调用 rper函数打印字符串,repr函数返回的字符串是加上了转义序列,是直接书写的字符串的形式

repr() 函数   返回一个对象的 string 格式。

实例

以下展示了使用 repr() 方法的实例:

>>>s = 'RUNOOB'

>>> repr(s)

 "'RUNOOB'"

 >>> dict = {'runoob': 'runoob.com', 'google': 'google.com'};

>>> repr(dict)

 "{'google': 'google.com', 'runoob': 'runoob.com'}"

 

2、%s 调用 str函数打印字符串,str函数返回原始字符串

 练习代码

#格式化字符    %d(格式化整数)
x = "There are %d types of people." % 10
binary = "binary"
do_not = "don't"
#格式化字符    %s(格式化字符串)替换后面的两个变量或值 
y = "Those who know %s and those who %s." % (binary, do_not)

print (x)
print (y)

#格式化字符    %r
print ("I said: %r." % x)
print ("I also said: '%s'." % y)
hilarious = False
joke_evaluation = "Isn't that joke so funny?! %r"

print (joke_evaluation % hilarious)

w = "This is the left side of..."
e = "a string with a right side."

print (w + e)

打印结果

笨办法学python 习题6:字符串(string)和文本