Python Program to Print Character (With Example)

Here, we will learn to write python program to print character using built-in methods – print() and sys.stdout.write().

At first, let’s see how these method works –
Tutorialwing Python Program to Print Character With Example

As we can see, value entered in console is stored in some variable first. Then, using built-in methods, we print it to console.

We have already covered how to take input as character in python. Now, we will see how to print using below methods –

  1. Using print() method
  2. Using sys.stdout.write() method

Method 1: Using print() to Print Character in Python

built-in method print() can be used to print character taken as input

a = input("Enter a character")[0]
print("Entered Character: ", a)

Notice that we have used input() method and stored only first character to variable a.

When we run above program, output is

Enter a character
H
Entered Character:  H

Method 2: Using sys.stdout.write() method

Using method – sys.stdout.write() present inside sys module in python, we can print character entered by user to console as shown below –

import sys
a = input("Enter a character")[0]
sys.stdout.write("Entered Character: " + a)

When we run above program, output is:

Enter a character
H
Entered Character: H

That’s how we can write python program to print character.

Leave a Reply