Python Print() - DigitalOcean
Maybe your like
Python Print
Almost all of our previous tutorial contains the Python print() function. But we did not discussed about python print function to the fullest. Now we will learn it. At first we should know about the basic structure of python print function. That is given below;
If you read our tutorial on Python Functions and arguments, then you should probably had the idea about the above function. The values receives a list of undefined variables. So, all the comma separated values will goes under the list values. So if you add more elements separated by comma, you will get a output where all the values are put together separated by space. The following example will guide you about the simple python print function usage.
The output of the following code will be.
1 string-2 23.42So, as many item you want to print, just put them together as arguments.
Using sep Keyword in python print function
If see the example of the previous section, you will notice that that variables are separated with a space. But you can customize it to your own style. Suppose in the previous code, you want to separate the values using underscore(_). Then you should pass underscore as the value of sep keyword. The following function will illustrate you the idea of using python print sep keyword.
# initiaze 1st variable var1 = 1 # initialize 2nd variable var2 = 'string-2' # initialize 3rd variable var3 = float(23.42) print(var1, var2, var3, sep='_')And you will get your desired output like this.
1_string-2_23.42Python print end keyword
The end key of print function will set the string that needs to be appended when printing is done. By default the end key is set by newline character. So after finishing printing all the variables, a newline character is appended. Hence, we get the output of each print statement in different line. But we will now overwrite the newline character by a hyphen(-) at the end of the print statement. See the following example.
# initialize a list initList = ['camel', 'case', 'stop'] # print each words using loop print('Printing using default print function') for item in initList: print(item) # default print function. newline is appended after each item. print() # another newline # print each words using modified print function print('Printing using modified print function') for item in initList: print(item, end='-')And you will get outputs like the following
Printing using default print function camel case stop Printing using modified print function camel-case-stop-Python print to file
In this section we will learn about file keyword. Actually file keyword is used for extracting output to a specified file. If you read our previous tutorial Python file operation, then you should know about basic file operation. So, you have to open a file in writable mode first, then use the file pointer as the value of file keyword in print() function. See the following code to understand the python print file usage.
# open a file in writable mood fi = open('output.txt', 'w') # initialize a list initList = ['camel', 'case', 'stop'] # print each words using loop print('Printing using default print function') for item in initList: print(item, file=fi) # use file keyword print(file=fi) # print each words using modified print function print('Printing using modified print function') for item in initList: print(item, end='-', file=fi) # use file keyword # close the file fi.close()And you will get the same output as the previous example in an output text file. That’s all about Python print. Hope that you understood it well. For any further query, feel free to use the comment section. Best of luck.
Tag » How To Print Words Python
-
Print() And Standard Out
-
How Do You Print A Word In Python? - Quora
-
Python Program To Print Even Length Words In A String - GeeksforGeeks
-
How Do I Print A String One Word At A Time In Python? - Stack Overflow
-
Your Guide To The Python Print() Function
-
How To Print Words In Python - Using Print Function - YouTube
-
Python Print Variable – How To Print A String And Variable
-
Python Print() Function - W3resource
-
Python: Convert The String To A List And Print All The Words And Their ...
-
Python Program To Print Even Length Words In A String - Tutorialspoint
-
Printing Words Vertically In Python
-
Python Program To Print Words Having More Than 5 Characters
-
Python Print() Function: How To Use Print Statement With Examples
-
Print Words Vertically - LeetCode