Ways to increment a character in Python
Last Updated :
17 Feb, 2023
In python there is no implicit concept of data types, though explicit conversion of data types is possible, but it not easy for us to instruct operator to work in a way and understand the data type of operand and manipulate according to that. For e.g Adding 1 to a character, if we require to increment the character, an error instructing type conflicts occur, hence other ways need to be formulated to increment the characters.
Python
s = 'M'
s = s + 1
print (s)
|
Output:
Traceback (most recent call last):
File "/home/fabc221bf999b96195c763bf3c03ddca.py", line 9, in
s = s + 1
TypeError: cannot concatenate 'str' and 'int' objects
Using ord() + chr()
Python3
ch = 'M'
x = chr ( ord (ch) + 3 )
print ( "The incremented character value is : " ,end = "")
print (x)
|
Output
The incremented character value is : P
Explanation : ord() returns the corresponding ASCII value of character and after adding integer to it, chr() again converts it into character.
Using byte string
Python3
ch = 'M'
ch = bytes(ch, 'utf-8' )
s = bytes([ch[ 0 ] + 10 ])
s = str (s)
print ( "The value of M after incrementing 10 places is : " ,end = "")
print (s[ 2 ])
|
Output
The value of M after incrementing 10 places is : W
Explanation : The character is converted to byte string , incremented, and then again converted to string form with prefix “‘b”, hence 3rd value gives the correct output.
Use the string module:
One additional approach to incrementing a character in Python is to use the string module’s ascii_uppercase or ascii_lowercase constant, depending on the case of the character you want to increment. These constants contain the uppercase or lowercase ASCII letters, respectively, as a string. You can then use the index method of the string to find the index of the character you want to increment, add 1 to that index, and use the resulting index to retrieve the next character from the appropriate constant.
Here is an example of how you could use this approach:
Python3
import string
ch = 'M'
if ch.isupper():
letters = string.ascii_uppercase
else :
letters = string.ascii_lowercase
index = letters.index(ch)
next_char = letters[index + 1 ]
print (f "The next character after {ch} is {next_char}" )
|
Output
The next character after M is N
Auxiliary space: O(1), or constant space. The code uses a fixed number of variables, regardless of the size of the input. Specifically, the code uses:
- The variable ch stores a single character.
- The variable letters to store a list of either uppercase or lowercase letters, depending on the case of ch.
- The variable index to store the index of ch in letters.
- The variable next_char to store the character after ch in letters.
Since the number of variables used is fixed and does not depend on the size of the input, the space complexity of the code is O(1).
Time complexity: O(1), or constant time. The code performs a fixed number of operations regardless of the size of the input. Specifically, the code:
- Initializes the variable ch with a single character.
- Determines whether ch is an uppercase or lowercase character.
- Finds the index of ch in the list of uppercase or lowercase letters.
- Increments the index and retrieves the character at the new index.
- Prints a message with the original character and the next character.
Since the number of operations is fixed and does not depend onthe size of the input, the time complexity of the code is O(1).
This will output The next character after M is N. Note that this approach will only work for ASCII letters and will not work for other characters or non-ASCII letters.
Please Login to comment...