Open In App

How to Take Only a Single Character as an Input in Python

Last Updated : 10 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Through this article, you will learn how to accept only one character as input from the user in Python.

Prompting the user again and again for a single character

To accept only a single character from the user input:

  • Run a while loop to iterate until and unless the user inputs a single character.
  • If the user inputs a single character, then break out of the loop.
  • Else the user gets prompted again and again.

Python3




usrInput = ''
 
while True:
    usrInput = input("Enter a single character as input: ")
    if len(usrInput) == 1:
        print(f'Your single character input was: {usrInput}')
        break
    print("Please enter a single character to continue\n")


Output:

Enter only a single character: adsf
Please enter a single character to continue

Enter only a single character: 2345
Please enter a single character to continue

Enter only a single character: 
Please enter a single character to continue

Enter only a single character: a
Your single character input was: a

The time complexity is O(1),  because the loop will execute only once for valid input, which consists of a single character.

The auxiliary space complexity of this code is constant O(1), because it only uses a fixed amount of memory to store the input character and the output message.

This program kept running until the user enter only a single character as input. If the user inputs a single character it prints the character and breaks out of the loop and the program finishes.

Using only one character from the user input string

This approach will use the string indexing to extract only a single required character from the user input.

Using only the first character of the string:

Python3




usrInput = input('Please enter a character:')[0]
print(f'Your single character input was: {usrInput}')


Output:

Please enter a character:adfs
Your single character input was: a

Using the ith character of the string:

Python3




i = 7
 
usrInput = input('Please enter a character:')[i]
print(f'Your single character input was: {usrInput}')


Output:

Please enter a character:0123456789
Your single character input was: 7

Using the last character of the string:

Python3




usrInput = input('Please enter a character:')[-1]
print(f'Your single character input was: {usrInput}')


Output:

Please enter a character:dfsg
Your single character input was: g

The time complexity of this code is constant, O(1), because the input string has only one character and the indexing operation to retrieve the last character takes constant time.

The space complexity is also constant, O(1), because the only memory used is for the variable usrInput, which stores a single character.



Previous Article
Next Article

Similar Reads

Take Matrix input from user in Python
Matrix is nothing but a rectangular arrangement of data or numbers. In other words, it is a rectangular array of data or numbers. The horizontal entries in a matrix are called as 'rows' while the vertical entries are called as 'columns'. If a matrix has r number of rows and c number of columns then the order of matrix is given by r x c. Each entrie
3 min read
Take input from stdin in Python
In this article, we will read How to take input from stdin in Python. There are a number of ways in which we can take input from stdin in Python. sys.stdininput()fileinput.input()Read Input From stdin in Python using sys.stdin First we need to import sys module. sys.stdin can be used to get input from the command line directly. It used is for stand
2 min read
How to take integer input in Python?
In this post, We will see how to take integer input in Python. As we know that Python's built-in input() function always returns a str(string) class object. So for taking integer input we have to type cast those inputs into integers by using Python built-in int() function. Let us see the examples: Example 1: C/C++ Code # take input from user input_
2 min read
Take input from user and store in .txt file in Python
In this article, we will see how to take input from users and store it in a .txt file in Python. To do this we will use python open() function to open any file and store data in the file, we put all the code in Python try-except block. Let's see the implementation below. Stepwise Implementation Step 1: First, we will take the data from the user and
2 min read
Python | Replace multiple occurrence of character by single
Given a string and a character, write a Python program to replace multiple occurrences of the given character by a single character. Examples: Input : Geeksforgeeks, ch = 'e' Output : Geksforgeks Input : Wiiiin, ch = 'i' Output : WinReplace multiple occurrence of character by singleApproach #1 : Naive Approach This method is a brute force approach
4 min read
Python | Convert Character Matrix to single String
Sometimes, while working with Python strings, we can have an option in which we need to perform the task of converting a character matrix to a single string. This can have applications in domains in which we need to work with data. Let us discuss certain ways in which we can perform this task. Method #1 : Using join() + list comprehension The combi
4 min read
Python program to read character by character from a file
Python is a great language for file handling, and it provides built-in functions to make reading files easy with which we can read file character by character. In this article, we will cover a few examples of it. Example Input: GeeksOutput: G e e k sExplanation: Iterated through character by character from the input as shown in the output.Read a fi
2 min read
Python - Create a Dictionary with Key as First Character and Value as Words Starting with that Character
In this article, we will code a python program to create a dictionary with the key as the first character and value as words starting with that character. Dictionary in Python is an unordered collection of data values, used to store data values like a map, which unlike other Data Types that hold only a single value as an element, Dictionary holds t
6 min read
Python | Insert character after every character pair
Sometimes, we can have a problem in which we need to insert a specific character after a pair(second) characters using Python. This kind of problem can occur while working with data, that require insertion of commas or any other special character mainly in the Machine Learning domain. Let's discuss certain ways in which this problem can be solved.
6 min read
Python | Pandas TimedeltaIndex.take()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas TimedeltaIndex.take() function return a new Index of the values selected by the indices. We generally pass a list of indices to b
2 min read
Practice Tags :
three90RightbarBannerImg