当前位置:网站首页>Explain Python input() function: get user input string

Explain Python input() function: get user input string

2020-11-09 10:53:00 Fog fog

input() yes  Python  Built in functions for , Used to read user input from the console .input() Functions always handle user input in the form of strings , So user input can contain any character .

input() The usage of the function is :

str = input(tipmsg)

explain :

  • str Represents a variable of type string ,input Will read the string into str in .
  • tipmsg Indicates a prompt message , It will be displayed on the console , Tell the user what to type in ; If you don't write tipmsg, There won't be any hint .


【 example 】input() Simple use of function :

a = input("Enter a number: ")
b = input("Enter another number: ")

print("aType: ", type(a))
print("bType: ", type(b))

result = a + b
print("resultValue: ", result)
print("resultType: ", type(result))

Examples of run results :

Enter a number: 100
Enter another number: 45
aType:  <class 'str'>
bType:  <class 'str'>
resultValue:  10045
resultType:  <class 'str'>

Press enter , After pressing enter input() Reading is over .

In this case, we enter two integers , I want to work out their sum , But it didn't work out ,Python It's just that they're like strings ,+ It plays the role of splicing strings , It's not summation .

We can use Python The built-in function converts a string to the desired type , such as :

  • int(string) Converts a string to int type ;
  • float(string) Converts a string to float type ;
  • bool(string) Converts a string to bool type .


Modify the above code , Convert user input into numbers :

a = input("Enter a number: ")
b = input("Enter another number: ")
a = float(a)
b = int(b)
print("aType: ", type(a))
print("bType: ", type(b))

result = a + b
print("resultValue: ", result)
print("resultType: ", type(result))

Running results :

Enter a number: 12.5
Enter another number: 64
aType:  <class 'float'>
bType:  <class 'int'>
resultValue:  76.5
resultType:  <class 'float'>

About Python 2.x

The above explanation is Python 3.x in input() Usage of , But in the older Python 2.x The situation is different in China .Python 2.x Two input functions are provided , Namely input() and raw_input():

  • Python 2.x raw_input() and Python 3.x input() The effect is the same , Can only read user input in the form of string .
  • Python 2.x input() It seems a little strange , It requires that the user's input must conform to Python The grammar of , A little carelessness makes a mistake , Generally speaking, it can only be an integer 、 decimal 、 The plural 、 String, etc. .


What's more compelling is ,Python 2.x input() The user is required to enter a string in quotation marks , This is against it. Python The principle of simplicity and ease of use , therefore Python 3.x This input mode is canceled .

Modify the first paragraph of this section , Get rid of print The brackets that follow :

a = input("Enter a number: ")
b = input("Enter another number: ")

print "aType: ", type(a)
print "bType: ", type(b)

result = a + b
print "resultValue: ", result
print "resultType: ", type(result)

stay Python 2.x Run the code under :

Enter a number: 45
Enter another number: 100
aType:  <type 'int'>
bType:  <type 'int'>
resultValue:  145
resultType:  <type 'int'>

End benefits

Here is a free set of 2020 newest python Introduction to the advanced project video tutorial , Click here to claim ! Or join the blogger Python Circle of communication (QQ Group :1049600759) As a free download .


 

The text and pictures of this article come from the Internet plus my own ideas , Just for learning 、 Communication use , Not for any commercial purpose , The copyright belongs to the original author , If you have any questions, please contact us in time for handling .

版权声明
本文为[Fog fog]所创,转载请带上原文链接,感谢