python函数示例_使用Python中的示例的input()函数

python函数示例

Python input()函数 (Python input() function)

input() function is a library function, it is used to get the user input, it shows given message on the console and wait for the input and returns input value in string format.

input()函数是一个库函数,用于获取用户输入,它在控制台上显示给定的消息并等待输入,并以字符串格式返回输入值。

Syntax:

句法:

    input(prompt)

Parameter(s): promot – a string value/message that will display on the console.

参数: promot –将在控制台上显示的字符串值/消息。

Return value: str – it returns user input in string format.

返回值: str –以字符串格式返回用户输入。

Example:

例:

    Input:
    text = input("Input any text: ")
    
    # if user input is "Hello world!"
    print("The value is: ", text)
    
    Output:
    Input any text: Hello world!

Example 1: Input a string and print it and its type

示例1:输入一个字符串并打印它及其类型

# python code to demonstrate example
# of input() function

# user input
text = input("Input any text: ")

# printing value 
print("The value is: ", text)

# printing type
print("return type is: ", type(text))

Output

输出量

Input any text: Hello world!
The value is:  Hello world!
return type is:  <class 'str'>

Example 2: Input different of types of values and print them along with their types

示例2:输入不同类型的值,并将其与它们的类型一起打印

# python code to demonstrate example
# of input() function

# input 1
input1 = input("Enter input 1: ")
print("type of input1 : ", type(input1))
print("value of input1: ", input1)

# input 2
input2 = input("Enter input 2: ")
print("type of input2 : ", type(input2))
print("value of input2: ", input2)

# input 3
input3 = input("Enter input 3: ")
print("type of input3 : ", type(input3))
print("value of input3: ", input3)

Output

输出量

Enter input 1: 12345
type of input1 :  <class 'str'>
value of input1:  12345
Enter input 2: 123.45
type of input2 :  <class 'str'>
value of input2:  123.45
Enter input 3: Hello [email protected]#$
type of input3 :  <class 'str'>
value of input3:  Hello [email protected]#$

输入整数和浮点值 (Input integer and float values)

There is no direct function that can be used to take input in either in an integer value or in a float value.

没有直接函数可用于以整数值或浮点值进行输入。

input() function takes an input from the user of any type like integer, float, string but returns all values in string format. If we need values in an integer or float types, we need to convert them.

input()函数从用户获取任何类型的输入,例如整数,浮点数,字符串,但以字符串格式返回所有值。 如果我们需要整数或浮点类型的值,则需要对其进行转换。

To convert input in an integer, we use int() function.

为了将输入转换为整数,我们使用int()函数

To convert input in a float, we use float() function.

为了将输入转换为浮点数,我们使用float()函数

Example 3: Python code to input integer and float values

示例3:用于输入整数和浮点值的Python代码

# python code to demonstrate example
# of input() function

# input an integer value
num = int(input("Enter an integer value: "))
print("type of num: ", type(num))
print("value of num: ", num)

# input a float value
num = float(input("Enter a float value: "))
print("type of num: ", type(num))
print("value of num: ", num)

Output

输出量

Enter an integer value: 12345
type of num:  <class 'int'>
value of num:  12345
Enter a float value: 123.45
type of num:  <class 'float'>
value of num:  123.45


翻译自: https://www.includehelp.com/python/input-function-with-example.aspx

python函数示例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值