In this article, we are going to learn how to use print() and sys.stdout.write() methods to write python program to print string.
Above image depicts how use built-in methods (input() or sys.stdin.read()) to take input and store in variable. Then, print that variable using another built-in method (print() or sys.stdout.write()).
Methods we are going to learn –
Method 1: Using print() method to Print String
We can using print() method as below –
import sys s = input("Enter string value: ") print("Entered string: ", s)
Here, we write string to console using print() method.
When you run above program, output is –
Enter string value: Hello Entered string: Hello
Method 2: Using sys.stdout.write() method to Print String
sys.stdout.write() is present inside sys module in python. We can use it as below –
import sys s = input("Enter string value: ") sys.stdout.write("Entered string: " + s)
When you run above program, output is –
Enter string value: Hi Tutorialwing! Entered string: Hi Tutorialwing!
That’s how we write python program to print string value.
You must be logged in to post a comment.