Skip to content

How to take string input in Python | Example code

  • by

You can take input in Python by using the raw_input() or the input() function command. The input() function reads the input and assigns whatever datatype it thinks best suits the input.


prompt:- A String, representing a default message before the input.

Note: raw_input() function has been discontinued in Python 3 and is only available in Python 2.

Example take string input in Python

A simple example code takes input from the user and returns (print) it.

name = input("Enter your name: ")



How to take string input in Python

Keep in mind that the input() function always returns a string, regardless of what the user enters. If you need to process the input as a different data type (e.g., an integer or a float), you’ll need to perform the appropriate conversion using functions like int() or float().

Another example

# Prompt the user to enter their name
name = input("Enter your name: ")

# Greet the user
print("Hello, " + name + "! Welcome to the program.")

# Prompt the user to enter their favorite color
color = input("What is your favorite color? ")

# Print a message using the user's input
print("Wow, " + name + "! " + color + " is a great choice!")

# Calculate the length of the user's name
name_length = len(name)

# Display the length of the user's name
print("By the way, your name has", name_length, "characters.")

When you run this code, it will ask the user to enter their name. After the user enters their name and presses Enter, it will display a greeting message using their name. Then, it will ask for their favorite color and display a message using both the name and the color. Finally, it will calculate and display the length of the user’s name.

Do comment if you have any doubts or suggestions on this Python input program.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading