Working with Files in Python - RRTutors
Last updated Aug 13, 2021Python Files
File Handling in Python is one of the best and amazing concepts. We can open, read, write, update, delete, create and close files in Python Programming Language. Files in Python are saved with an extension of .txt. File handling is one of the best options for controlling files in your python programming as it builds a platform for using and modifying files.
Displaying data to the Screen
In python, the print() function is used to display constant, variable, or data on screen and debug the code. Print() function content can be used as follows:-
- Print statement to the print variable, constant on screen.
- Commas are used to separate variables and print them.
- String (sequence of characters, which means it is an ordered collection of other values) formatting is done with help of % or {}.
- + Operator to separate variables in the print statement.
- F-string to print statements.
Simple example of using print() function :-
print("I like python and want to learn more about it")
|
I like python and want to learn more about it |
How to Read User Input from console in Python?
Python uses two inbuilt functions to take input from the user which comes from the keyboard, Those two in build functions are:-
raw_input - raw_input is a form of input that takes the argument in the form of a string. An example is mentioned below:-
name=raw_input('Enter your name: ') print ("Hi %s, Learn Python with me!" % name); |
Enter your name: Superman Hi Superman, Learn Python with me! |
input() - The input() method reads the user's input and returns it as a string but the main difference between the raw_input and input function is the return type of raw_input is always string whereas input can take input as a variable, constant also.
Examples are mentioned below:-
age = input('Enter Your Age: ') |
Enter Your Age: 25 |
These are the basic operations that you can perform for file handling python. You can also ope, read, write, close, modify, delete these files according to your needs.
Article Contributed By :
|
|
|
|
103 Views |