How to take only a single character as an input in Python

In this tutorial, we will learn how to take only a single character as an input in Python with some cool and easy examples. In many situations, you might have to come up with this type of requirements.

I know you are here just because you are in need of this awesome trick to take only a single character as an input in Python ignoring all the characters entered by the user except the first character.

If you don’t know how to ignore all the characters of user input except the first character then you are at the right place. Because in this tutorial we gonna find out how to accept only first character as an user input in Python.

Take only a single character as an input in Python

Let’s learn this with some easy examples,

At first, create a variable which can hold the value entered by the user. ( user input )

user_input = input('Enter Your Characters\n')
print(user_input)

Here \n is used just to create a new line so that it looks nice. If you wish you can omit it.
Run it online
If you run this program the output will be like this:

Enter Your Characters

Now in the console log, you can enter characters and the characters will be stored in the variable user_input

You can simply use

print(user_input[0])

to print only the first character. That will work fine.

But our main aim is to get the only first character as an input. But if we use the above code we can not achieve our goal. We are just printing the first character from the user input characters. But all the characters entered by the user is being stored in the user_input variable.

We need to find a way to store only the first character from the given input entered by the user in the console log.

The below code will help us to achieve our goal

user_input = input('Enter Your Characters\n')[0]
print(user_input)

Run this code online
Now if you run the code it will ask you to enter characters. But it will not store all the characters in that variable. It will just store the first character from the user input entered by the user in the console log.

Suppose you run the above program and entered “ufayuyosuh” as an input. It will store only “u” in the variable.

user_input = input('Enter Your Characters')[n-1]

Here the n-1 surrounded with the square brackets is the index of the character entered by the user. You can change the value of n to store your required character. If you wish to take only the 3rd character from the input then use 2 instead of n-1. because of 3-1=2

You may also learn,

Take only the first character from the user input in Python

Let’s see this example,

user_input = input('Enter Your Characters\n')[0]
print("\nYour variable is\n")
print(user_input)

Run this program online
Output example,

Enter Your Characters
soikhdhskhsgjs
Your variable is
s

3 responses to “How to take only a single character as an input in Python”

  1. MCh says:

    Using the input function means having to terminate the string with a .

    Is there a means of reading a single character input via the keyboard in non-windows code? I’m using IDLE to run Python3 code.

  2. Rajat Ranjan says:

    How to take input from the user as a character and check the given is vowel or not us

    • Nikos Kount says:

      character = input(“Enter a character\n”)[0]
      vowels = “aeiou”
      if character in vowels:
      print(character)

Leave a Reply

Your email address will not be published. Required fields are marked *