How to Take Only a Single Character as an Input in Python
Last Updated :
28 Apr, 2025
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.
Similar Reads
Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Input and Output in Python Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython's input() function
7 min read
Python basic I/O Techniques
Taking input in Python Developers often have a need to interact with users, either to get data or to provide some sort of result. Most programs today use a dialog box as a way of asking the user to provide some type of input. While Python provides us with two inbuilt functions to read the input from the keyboard. input ()
3 min read
Python input() Function Python input() function is used to take user input. By default, it returns the user input in form of a string.input() Function Syntax: input(prompt)prompt [optional]: any string value to display as input messageEx: input("What is your name? ")Returns: Return a string value as input by the user.By de
4 min read
Taking input from console in Python What is Console in Python? Console (also called Shell) is basically a command line interpreter that takes input from the user i.e one command at a time and interprets it. If it is error free then it runs the command and gives required output otherwise shows the error message. A Python Console looks
2 min read
Python - Print Output using print() function Python print() function prints the message to the screen or any other standard output device. In this article, we will cover about print() function in Python as well as it's various operations.Python# print() function example print("GeeksforGeeks") a = [1, 2, 'gfg'] print(a) print() Function Syntax
4 min read
Python - Output Formatting In Python, output formatting refers to the way data is presented when printed or logged. Proper formatting makes information more understandable and actionable. Python provides several ways to format strings effectively, ranging from old-style formatting to the newer f-string approach.Formatting Out
5 min read
Python3 I/O
Different Input and Output Techniques in Python3 An article describing basic Input and output techniques that we use while coding in python. Input Techniques 1. Taking input using input() function -> this function by default takes string as input. Example: Python3 #For string str = input() # For integers n = int(input()) # For floating or deci
3 min read