python3.4 user input and output control method

Today small for everyone to share user input and the output of an python3.4 control method, a good reference value, we want to help. Come and see, to follow the small series together
a input

1. Function Format: input ()

2. Function: accepts a standard input data, return a string type. ctrl + z input end.

3. Example:

默认input():等待一个任意字符的输入
 
str=input(‘input a string:\n'):接受输入数据作为string类型传给str,\n为提示信息换行。

4. Accept a plurality of data inputs using eval () function, the interval must be a comma character

>>> lines
['', '', '', '84', '2', '3', '']
>>> a,b,c=eval(input())
1,2,3
>>> a
1
>>> c
3

5. A method of receiving a multi-line input

>>> sen='end'  #作为结束符
>>> list2=[]
 
>>> for line in iter(input,ends):
line1=line.split(',')
list2.append(line1)
 
 
23,34
25,78
end
>>> list2
[['23', '34'], ['25', '78']]

Second, the output

1. Function Format: print ([object, ...], sep = '', end = '\ n', file = sys.stdout) (end Enter the default value, can be custom symbol)

2. Examples

print (): outputs a blank line

Formatted output

#% X - hex hex

#% D - dec Decimal

#% O - oct octal

#% S - string

#% f - float float
Example 1:

>>> str1='the value is'
>>> num1=11
>>> print('%s%d'%(str1,num1))
the value is11

Example 2:

PI=3.1415926
 
print("PI = %10.3f" % math.pi) #输出PI =  3.142
 
print("PI = %-10.3f" % math.pi) #输出PI = 3.142

Example 3:

print("%.3s" %("abcde")) #输出abc
 
print("%.*s" %(4,"abcde")) #输出abcd
 
print("%10.3s" %("abcde")) #输出  abc(总长度为10,字符长度不够前面填空格)

Example 4: printing a plurality of rows

print(""" 你的内容 """) 或者 print(''' 你的内容 ''')

Example 5: print plain text, without the use of escape characters: print (r 'content') or print (R 'content')

print(r'abc\n') #直接打印字符串abc\n

We recommend the python learning sites to see how seniors are learning! From basic python script, reptiles, django, data mining, programming techniques, as well as to combat zero-based sorting data items, given to every love learning python small partner! Python veteran day have to explain the timing of technology, to share some of the ways to learn and need to pay attention to small details, click on Join us python learner gathering
methods above this python3.4 control user input and the output is small series for everyone to share the entire contents of the, we hope to give you a reference

Released six original articles · won praise 0 · Views 1310

Guess you like

Origin blog.csdn.net/haoxun06/article/details/104365864