Print() Function In Python
Print function allows you to output your data in the place where your program will display its result. For example, after your program did a process, you can include print() function in order to show a message to inform the user that the process is complete or you can show the user an error message.
#____ProgrammerCodes.com____ print() #____ProgrammerCodes.com____
#____ProgrammerCodes.com____ print("Hi everyone") #____ProgrammerCodes.com____
#____ProgrammerCodes.com____ age = 19 print("Her age is: ", age) #____ProgrammerCodes.com____
Her age is: 19
Input() function in Python?
With Input function, you will be able to get data from the users or any kind of system and it will return to you that data in order to do something with it such as output the data from the input function or passing the input data to another function.
#____ProgrammerCodes.com____ input() #____ProgrammerCodes.com____
#____ProgrammerCodes.com____ name = input("What is your name: ") print("Welcome", name) #____ProgrammerCodes.com____
Welcome (Here You Will Get The Data That You Entered In The Input Function)
How to open, read, write, and close a file in Python?
Dealing with files is a famous process in Python programming language and a lot of people use files functions and methods to read and access or write new data in a file.
How to write in a text file using Python?
#____ProgrammerCodes.com____ file = open("myfile.txt", "w") file.write( "This is my new text file using Python\nThank you ProgrammerCodes.com\n") file.close() #____ProgrammerCodes.com____
file = open(“myfile.txt”, “w”)
We assigned a variable that contains the file’s open function and we add to it the parameter (w) that will allow us to write in the file and create a new file if it does not exist.
This (w) will delete the old text that exists in the file and write a new text. (“myfile.txt”) is the name of the file that we want to create or to write inside it.
The text file can exist in the same directory of the python file or in the next levels of folders directories. for example (“files/myfile.txt”) This is the directory of the text file that exists in a folder inside the folder of Python file.
file.write( “This is my new text file using Python\nThank you ProgrammerCodes.com\n”)
The function write() will help you write the text that you want and adding it to the file. You have to add ( \n ) to go to the newt line.
If we want to open the file and write in this file but we want to keep the old text and append to it, we have to change the access mode parameter from (w) to (a).
#____ProgrammerCodes.com____ file = open("myfile.txt", "a") file.write( "This is my new text file using Python\nThis is a new phrase.") file.close() #____ProgrammerCodes.com____
Don’t forget to close the file after you have completed the writing process.
How to read a text file in Python?
To read a text file using Python programming language, you should do these steps:
- Open the text file.
- Write the name of the text file.
- Set the access mode parameter to (r) or (r+) [the default access mode is (r)].
- Call the function read() and add bytes in order to get the location of the data that you want to read from the file and.
- Close the file.
#____ProgrammerCodes.com____ file = open("myfile.txt", "r+") data = fo.read(10); print(data) file.close() #____ProgrammerCodes.com____
Reading text file line by line in Python
To read a text file line by line; you should use for loop and open file function.
#____ProgrammerCodes.com____ file = open("files/myfile.txt", "r") for line in file: print(line.strip()) file.close() #____ProgrammerCodes.com____