YouTip LogoYouTip

Python Func Raw_Input

Python raw_input() Function | Tutorial

Tutorial -- Learning is not just about technology, but also about dreams!

Python Basic Tutorial

Python Basic Tutorial Python Introduction Python Environment Setup Python Chinese Encoding Python VS Code Python Basic Syntax Python Variable Types Python Operators Python Conditional Statements Python Loop Statements Python While Loop Statement Python for Loop Statement Python Nested Loops Python break Statement Python continue Statement Python pass Statement Python Number Python Strings Python Lists Python Tuples Python Dictionary Python Date and Time Python Functions Python Modules Python File I/O Python File Methods Python Exceptions Python OS File/Directory Methods Python Built-in Functions

Python Advanced Tutorial

Python Object-Oriented Python Regular Expressions Python CGI Programming Python MySQL Database Operations Python Network Programming Python SMTP Email Sending Python Multithreading Python XML Parsing Python GUI Programming (Tkinter) Differences between Python2.x and 3.x Python IDE Python JSON Python AI Drawing Python 100 Examples Python Quiz

Python OS File/Directory Methods

Python Object-Oriented

Deep Dive

  • Computer Science
  • Development Tools
  • Scripting
  • Web Service
  • Scripting Languages
  • Network Design & Development
  • Programming Languages
  • Software
  • Network Services
  • Programming

Python2.x Python raw_input() Function

Python Built-in Functions Python Built-in Functions

The python raw_input() function is used to get input from the console.

raw_input() treats all input as a string and returns a string type.

Note: Both the input() and raw_input() functions can receive strings, but raw_input() reads the console input directly (it can receive any type of input). For input(), it expects to read a valid Python expression, meaning you must enclose strings in quotes when entering them, otherwise it will raise a SyntaxError.

Unless you have a specific need for input(), it is generally recommended to use raw_input() for user interaction.

Note: In python3, input() receives a str type by default.

Function Syntax

raw_input()

Parameter Description:

  • prompt: Optional. A string that can serve as a prompt.

Examples

raw_input() treats all input as a string

>>>a = raw_input("input:")
input:123
>>>type(a)
<type 'str'> # String
>>>a = raw_input("input:")
input:tutorial
>>>type(a)
<type 'str'> # String
>>>

input() requires a Python expression

>>>a = input("input:")
input:123 # Enter an integer
>>>type(a)
<type 'int'> # Integer
>>>a = input("input:")
input:"tutorial" # Correct, string expression
>>>type(a)
<type 'str'> # String
>>>a = input("input:")
input:tutorial # Error, not an expression
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'tutorial' is not defined
<type 'str'>

Python Built-in Functions Python Built-in Functions

← Python Func StaticmethodVue Component Custom Event β†’