logo

Funzione input() di Python

Funzione input() di Python viene utilizzato per ricevere l'input dell'utente. Per impostazione predefinita, restituisce l'input dell'utente sotto forma di stringa.

input() Funzione

Sintassi:



input(prompt)>

richiesta [facoltativo]: qualsiasi valore stringa da visualizzare come messaggio di input

Esempio: input(Come ti chiami? )

Ritorna: Restituisce un valore stringa come input dall'utente.



android.process.acore continua a fermarsi
By default input() function helps in taking user input as string. If any user wants to take input as int or float, we just need to typecast it.>

Python3






# Taking input as string> color>=> input>(>'What color is rose?: '>)> print>(color)> # Taking input as int> # Typecasting to int> n>=> int>(>input>(>'How many roses?: '>))> print>(n)> # Taking input as float> # Typecasting to float> price>=> float>(>input>(>'Price of each rose?: '>))> print>(price)>

sottostringa della stringa Java

>

>

Produzione:

What color is rose?: red red How many roses?: 10 10 Price of each rose?: 15.50 15.5>

Esempio 1: prendere IL Nome ed età dell'utente come input e stampa

Per impostazione predefinita, l'input restituisce una stringa. Quindi il nome e l'età verranno memorizzati come stringhe.

Pitone




# Taking name of the user as input> # and storing it name variable> name>=> input>(>'Please Enter Your Name: '>)> # taking age of the user as input and> # storing in into variable age> age>=> input>(>'Please Enter Your Age: '>)> print>(>'Name & Age: '>, name, age)>

>

>

nome della città negli Stati Uniti

Produzione:

Please Enter Your Name: Rohit Please Enter Your Age: 16 Name & Age: Rohit 16>

Esempio 2: prendere due numeri interi dagli utenti e aggiungerli.

In questo esempio, vedremo come ricevere input interi dagli utenti. Per ricevere l'input intero utilizzeremo int() insieme a Ingresso Python()

Pitone




# Taking number 1 from user as int> num1>=> int>(>input>(>'Please Enter First Number: '>))> # Taking number 2 from user as int> num2>=> int>(>input>(>'Please Enter Second Number: '>))> # adding num1 and num2 and storing them in> # variable addition> addition>=> num1>+> num2> # printing> print>(>'The sum of the two given numbers is {} '>.>format>(addition))>

>

>

Produzione:

numero primo java

Allo stesso modo, possiamo usare float() per prendere due numeri float. Vediamo un altro esempio di come prendere le liste come input

Esempio 3: prendere due elenchi come input e aggiungerli

Prendendo l'input dell'utente come una stringa e dividendolo su ciascun carattere utilizzando list() per convertirlo in un elenco di caratteri.

Pitone

selezione html




# Taking list1 input from user as list> list1>=> list>(>input>(>'Please Enter Elements of list1: '>))> # Taking list2 input from user as list> list2>=> list>(>input>(>'Please Enter Elements of list2: '>))> # appending list2 into list1 using .append function> for> i>in> list2:> >list1.append(i)> # printing list1> print>(list1)>

>

>

Produzione: