Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python Variables
Next article icon

Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution.

Taking input in Python

Python'sinput() function is used to take user input. By default, it returns the user input in form of a string. 

Example:

Python
name=input("Enter your name: ")print("Hello,",name,"! Welcome!")

Output

Enter your name: GeeksforGeeks
Hello, GeeksforGeeks ! Welcome!

The code prompts the user to input their name, stores it in the variable "name" and then prints a greeting message addressing the user by their entered name.

To learn more about taking input, please refer:Taking Input in Python

Printing Output using print() in Python

At its core, printing output in Python is straightforward, thanks to the print() function. This function allows us to display text, variables and expressions on the console. Let's begin with the basic usage of the print() function:

In this example, "Hello, World!" is a string literal enclosed within double quotes. When executed, this statement will output the text to the console.

Python
print("Hello, World!")

Output
Hello, World!

Printing Variables

We can use the print() function to print single and multiple variables. We can print multiple variables by separating them with commas.Example:

Python
# Single variables="Bob"print(s)# Multiple Variabless="Alice"age=25city="New York"print(s,age,city)

Output
BobAlice 25 New York

Take Multiple Input in Python

We are taking multiple input from the user in a single line, splitting the values entered by the user into separate variables for each value using thesplit() method. Then, it prints the values with corresponding labels, either two or three, based on the number of inputs provided by the user.

Python
# taking two inputs at a timex,y=input("Enter two values: ").split()print("Number of boys: ",x)print("Number of girls: ",y)# taking three inputs at a timex,y,z=input("Enter three values: ").split()print("Total number of students: ",x)print("Number of boys is : ",y)print("Number of girls is : ",z)

Output

Enter two values: 5 10
Number of boys: 5
Number of girls: 10
Enter three values: 5 10 15
Total number of students: 5
Number of boys is : 10
Number of girls is : 15

How to Change the Type of Input in Python

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 totypecast it.

Print Names in Python

The code prompts the user to input a string (the color of a rose), assigns it to the variable color and then prints the inputted color.

Python
# Taking input as stringcolor=input("What color is rose?: ")print(color)

Output

What color is rose?: Red
Red

Print Numbers in Python

The code prompts the user to input an integer representing the number of roses, converts the input to an integer using typecasting and then prints the integer value.

Python
# Taking input as int# Typecasting to intn=int(input("How many roses?: "))print(n)

Output

How many roses?: 88

Print Float/Decimal Number in Python

The code prompts the user to input the price of each rose as a floating-point number, converts the input to a float using typecasting and then prints the price.

Python
# Taking input as float# Typecasting to floatprice=float(input("Price of each rose?: "))print(price)

Output

Price of each rose?: 50.3050.3

Find DataType of Input in Python

In the given example, we are printing the type of variable x. We will determine the type of an object in Python.

Python
a="Hello World"b=10c=11.22d=("Geeks","for","Geeks")e=["Geeks","for","Geeks"]f={"Geeks":1,"for":2,"Geeks":3}print(type(a))print(type(b))print(type(c))print(type(d))print(type(e))print(type(f))

Output
<class 'str'><class 'int'><class 'float'><class 'tuple'><class 'list'><class 'dict'>

Output Formatting

Output formatting in Python with various techniques including the format() method, manipulation of thesep and end parameters, f-strings and the versatile % operator. These methods enable precise control over how data is displayed, enhancing the readability and effectiveness of your Python programs.

Example 1: Using Format()

Python
amount=150.75print("Amount: ${:.2f}".format(amount))

Output
Amount: $150.75

Example 2: Using sep and end parameter

Python
# end Parameter with '@'print("Python",end='@')print("GeeksforGeeks")# Seprating with Commaprint('G','F','G',sep='')# for formatting a dateprint('09','12','2016',sep='-')# another exampleprint('pratik','geeksforgeeks',sep='@')

Output
Python@GeeksforGeeksGFG09-12-2016pratik@geeksforgeeks

Example 3: Using f-string

Python
name='Tushar'age=23print(f"Hello, My name is{name} and I'm{age} years old.")

Output
Hello, My name is Tushar and I'm 23 years old.

Example 4: Using % Operator

We can use'%' operator. % values are replaced with zero or more value of elements. The formatting using % is similar to that of ‘printf’ in the C programming language.

  • %d –integer
  • %f – float
  • %s – string
  • %x –hexadecimal
  • %o – octal
Python
# Taking input from the usernum=int(input("Enter a value: "))add=num+5# Outputprint("The sum is%d"%add)

Output

Enter a value: 50The sum is 55

Taking Conditional User Input in Python

In Python, taking conditional user input means getting input from the user and making decisions based on that input. You usually use the input() function to get the value and then use if-else statements to check conditions.

  • input() is used to take user input as a string.
  • You can use int() or float() to convert it if needed.
  • Use if, elif, and else to apply conditions to the input.

Related Posts:

Recommended Problems:


print() in Python
Visit Courseexplore course icon
Video Thumbnail

print() in Python

Video Thumbnail

input() in Python

Improve
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp