Usando input() para recibir datos

Lesson 10/17 | Tiempo de estudio: 10 Min

Hasta ahora, solo mostrábamos información con print(). Ahora vamos a recibir datos del usuario.


¿Qué hace input()?

Muestra un mensaje y espera que el usuario escriba algo.

Siempre devuelve una cadena (str).

nombre = input("¿Cuál es tu nombre? ")
print("Hola", nombre, "¡bienvenido a Python!")


Convertir datos de entrada:

edad = int(input("¿Cuántos años tienes? "))
altura = float(input("¿Cuál es tu altura en metros? "))
print("Tienes", edad, "años y mides", altura, "metros.")


Formatear texto con f""

¿Qué es un f-string?

Es una forma moderna de insertar variables en texto.

Solo agrega f antes de las comillas.


Sin f-string:

nombre = "Ana"
edad = 25
print("Hola", nombre, "tienes", edad, "años.")


Con f-string:

print(f"Hola {nombre}, tienes {edad} años.")


Ventajas:

- Más claro y limpio.

- No necesitas usar comas ni +.


Puedes incluir operaciones dentro del texto:

print(f"El doble de tu edad es {edad * 2}")

Configuración de Cookies

When you visit any of our websites, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and manage your preferences. Please note, that blocking some types of cookies may impact your experience of the site and the services we are able to offer.