Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Ways to increment a character in python
In this tutorial, we are going to see different methods to increment a character in Python. Characters cannot be directly incremented like integers, so we need to convert them to their ASCII values first.
Why Direct Increment Fails
Let's first see what happens if we try to add an integer to a character directly without proper conversion ?
# str initialization char = "t" # try to add 1 to char char += 1 # gets an error
If you execute the above program, it produces the following result −
TypeError: must be str, not int
Using ord() and chr() Functions
To increment a character in Python, we have to convert it into an integer using ord(), add 1 to it, and then cast the resultant integer back to char using chr().
# str initialization
char = "t"
# converting char into int using ord()
i = ord(char)
# incrementing the ASCII value
i += 1
# casting the resultant int to char using chr()
char = chr(i)
print(f"Alphabet after t is {char}")
If you execute the above program, it produces the following result −
Alphabet after t is u
Using bytes() Method
There is another way to increment a character using bytes. Here's how it works ?
- Convert string into bytes
- The result will be an array containing ASCII values of all characters
- Add 1 to the desired character's ASCII value
- Convert the int back into char
# str initialization
char = "t"
# converting char to bytes
b = bytes(char, 'utf-8')
# adding 1 to first char's ASCII value
# b[0] gives ASCII value of 't'
incremented_ascii = b[0] + 1
# converting back to char
result = chr(incremented_ascii)
print(f"Alphabet after incrementing ASCII value is {result}")
If you execute the above program, it produces the following result −
Alphabet after incrementing ASCII value is u
Incrementing Specific Character in a String
If we have a string and want to increment a specific character at any position, we can use the bytes method to target that character ?
# str initialization
name = "tutorialspoint"
# converting string to bytes
b = bytes(name, 'utf-8')
# adding 1 to 'a' char at index 6
# 'a' index is 6 in "tutorialspoint"
incremented_char = chr(b[6] + 1)
# reconstructing the string
result = name[:6] + incremented_char + name[7:]
print(f"String after incrementing 'a' char: {result}")
If you execute the above program, it produces the following result −
String after incrementing 'a' char: tutoriblspoint
Comparison of Methods
| Method | Best For | Complexity |
|---|---|---|
ord() and chr()
|
Single characters | Simple and direct |
bytes() |
String manipulation | More complex but flexible |
Conclusion
Use ord() and chr() for simple character incrementing. The bytes() method is useful when working with strings and need to increment specific characters at particular positions.
