python01

 1. input()

Python3中不再沿用paython2中的raw_input(),使用input(),作用是读取控制台用户的输入数据

print("How old are you?"),
age = input()

print("How tall are you?"),
height = input()

print("How much do you weigh"),
weight = input()

print("You're %r old, %r tall and %r heavy." %(age, height, weight))
age = input("How old are you?")
height = input("How tall are you?")
weight = input("How much do you weigh")

print("So, you're %r old, %r tall and %r heavy." %(age, height, weight))

上面两种方式都可以,第二种更为简洁方便

2. %r与%s的区别

%r用rper()方法处理对象

%s用str()方法处理对象

有些情况下,两者处理的结果是一样的,比如说处理int型对象。

比如:

print("She is %s years old." % 18)
print("She is %r years old." % 18)
print("She is %d years old." % 18)

运行结果为:

She is 18 years old.
She is 18 years old.
She is 18 years old.

此处可以看到结果都是一样的

再看另外一个例子:

text = "I am 18 years old "
print("She said: %s." % text)
print("She said: %r." % text)

运行结果为:

She said: I am 18 years old .
She said: 'I am 18 years old '.

使用%r时,将字符串使用了单引号

3.  Python的其他格式化字符:

%% 百分号标记 #就是输出一个%
%c 字符及其ASCII码
%s 字符串
%d 有符号整数(十进制)
%u 无符号整数(十进制)
%o 无符号整数(八进制)
%x 无符号整数(十六进制)
%X 无符号整数(十六进制大写字符)
%e 浮点数字(科学计数法)
%E 浮点数字(科学计数法,用E代替e)
%f 浮点数字(用小数点符号)
%g 浮点数字(根据值的大小采用%e或%f)
%G 浮点数字(类似于%g)
%p 指针(用十六进制打印值的内存地址)
%n 存储输出字符的数量放进参数列表的下一个变量中

        
%格式化符也可用于字典,可用%(name)引用字典中的元素进行格式化输出。

原文地址:https://www.cnblogs.com/stacy828/p/9771428.html