python 怎么样才有output_Python - 3. Input and Output

from:http://interactivepython.org/courselib/static/pythonds/Introduction/InputandOutput.html

Input and Output

Input

Python’s input function takes a single parameter that is a string. This string is often called the prompt because it contains some helpful text prompting the user to enter something. For example, you might call input as follows:

aName = input('Please enter your name: ')

aName = input("Please enter your name ")

print("Your name in all capitals is",aName.upper(),

"and has length", len(aName))

It is important to note that the value returned from the input function will be a string representing the exact characters that were entered after the prompt. If you want this string interpreted as another type, you must provide the type conversion explicitly.

sradius = input("Please enter the radius of the circle ")

radius = float(sradius)

diameter = 2 * radius

Output (String Formatting)

>>> print("Hello")

Hello

>>> print("Hello","World")

Hello World

>>> print("Hello","World", sep="***")

Hello***World

>>> print("Hello","World", end="***")

Hello World***

>>>print(aName, "is", age, "years old.")

>>>print("%sis %dyears old." % (aName, age))

The % operator is a string operator called the format operator.

CharacterOutput Format

d, i

Integer

u

Unsigned integer

f

Floating point as m.ddddd

e

Floating point as m.ddddde+/-xx

E

Floating point as m.dddddE+/-xx

g

Use %e for exponents less than −4−4 or greater than +5+5 , otherwise use %f

c

Single character

s

String, or any Python data object that can be converted to a string by using the str function.

%

Insert a literal % character

In addition to the format character, you can also include a format modifier between the % and the format character. Format modifiers may be used to left-justify or right-justifiy the value with a specified field width.

ModifierExampleDescription

number

%20d

Put the value in a field width of 20

-

%-20d

Put the value in a field 20 characters wide, left-justified

+

%+20d

Put the value in a field 20 characters wide, right-justified

0

%020d

Put the value in a field 20 characters wide, fill in with leading zeros.

.

%20.2f

Put the value in a field 20 characters wide with 2 characters to the right of the decimal point.

(name)

%(name)d

Get the value from the supplied dictionary using name as the key.

>>> price = 24

>>> item = "banana"

>>> print("The %scosts %dcents"%(item,price))

The banana costs 24 cents

>>> print("The %+10scosts %5.2fcents"%(item,price))

The banana costs 24.00 cents

>>> print("The %+10scosts %10.2fcents"%(item,price))

The banana costs 24.00 cents

>>> itemdict = {"item":"banana","cost":24}

>>> print("The %(item)scosts %(cost)7.1fcents"%itemdict)

The banana costs 24.0 cents

>>>

In addition to format strings that use format characters and format modifiers, Python strings also include a format method that can be used in conjunction with a new Formatter class to implement complex string formatting. More about these features can be found in the Python library reference manual.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值