In this article, we will discuss about different ways in which we can write python program to find next character of alphabets with examples.
Getting Started
The task is to print next letter of alphabets from given letter in circles. For examples,
If user enters A, output should be –
B
If user enters B, output should be –
C
If user enters C, output should be –
D
If user enters Z, output should be –
A
Similarly,
User can also enters letter in lowercase. That should also work in similar manner. For example, If user enters a, output is b. If user enters z, output is a etc.
Please note that output for last letter in alphabets is first letter of alphabets. That’s what cs circle is about.
Now, how can we achieve this task ?
We can do so in many ways. Here are 7 different ways to find next letter in python –
- Using ord() and chr() Functions
- Using bytes() and str() Functions
- Using bytes() and chr() Functions
- Using ord() and Math Calculations – 1
- Using ord() and Math Calculations – 2
- Using ascii_lowercase and ascii_uppercase
Using ord() and chr() Functions
We can use ord() and chr() functions to get next character in alphabets as shown below –
Pseudo Algorithm
- Get the input from user. Then, find ASCII value of that character.
- Increment the ASCII value by 1 and store in any variable i.e. i
- Then, get character represented by incremented ASCII value using chr() function and print it using print() function.
ch = input("Enter a character") i = ord(ch) i = i + 1 nch = chr(i) print(nch)
Output:
Enter a character A B
In above program,
- Store entered value in variable ch.
- Get the ASCII value using ord() function and store in variable i.
- Increment value of i by 1.
- Find next character using chr() function and incremented value. Then, store it in variable nch
- Print next character using print() function
Above code has some problem. It does not output correct value if use enters last character of alphabets i.e. Z or z.
To fix this issue, we need to do some condition check for last character. For example,
We need to check if entered character is last character in alphabet or not. If it is last character, we need to set next character as first character of alphabets.
Modified Algorithm to Maintain Circle
- Get the input from user. Then, find ASCII value of that character.
- Increment the ASCII value by 1 and store in any variable i.e. i
- If entered value is last character, initialise ASCII value to first character of alphabets.
- Then, get character represented by incremented ASCII value using chr() function and print it using print() function.
Source Code
ch = input("Enter a character") i = ord(ch) if(i >= 90): # 90 is ASCII value of last character of alphabets in uppercase. i = 65 # 65 is ASCII value of first character of alphabets in uppercase. else: i = i + 1 nch = chr(i) print("Next character is", nch)
Output:
Enter a character Z Next character is A
Next Letter in Lowercase
If you want to print next letter in lowercase, we can do so as below –
ch = input("Enter a character") i = ord(ch) if(i >= 122): # 122 is ASCII value of last character of alphabets in lowercase. i = 97 # 97 is ASCII value of first character of alphabets in lowercase. else: i = i + 1 nch = chr(i) print("Next character is", nch)
Output:
Enter a character a Next character is b
Using bytes() and str() Functions
We can use bytes() and str() function to find and print next letters in alphabets as shown below –
ch = input("Enter a character") ch = bytes(ch, 'utf-8') # Convert character to byte nextCh = ch[0] + 1 # Add 1 to entered character if(nextCh > 90): # Check if entered character is last character nextCh = 65 # If yes, next character is first character s = bytes([nextCh]) s = str(s) print ("Next character is : ",end="") print (s[2])
Output:
Enter a character Z Next character is : A
In above program,
- Take input character from user.
- Convert into bytes and add 1 to it. Check if entered character is last character or not.
- If yes, next character will be first character.
- Then, convert it into bytes, then string and print it.
- During these conversion, prefix b is added with actual value. So, third place value gives us next character.
Print Next Character in Lowercase
We can modify condition check to print next letter in alphabets in lowercase as below –
## Lowercase ch = input("Enter a character") ch = bytes(ch, 'utf-8') nextCh = ch[0] + 1 if(nextCh >= 122): nextCh = 97 s = bytes([nextCh]) s = str(s) print ("Next character is : ",end="") print (s[2])
Output:
Enter a character z Next character is : a
Using bytes() and chr() Functions
In above program, we have converted back the bytes to string. Then, next character was retrieved using 3rd index from the string value.
We can modify above program and use chr() function instead to get next character in alphabets as shown below –
ch = input("Enter a character") ch = bytes(ch, 'utf-8') nextCh = ch[0] + 1 if(nextCh >= 90): nextCh = 65 print ("Next character is : ",end="") print(chr(nextCh))
Output:
Enter a character Z Next character is : A
In above program,
- Variable nextCh contains ASCII value of next character from entered character by user.
- We can get corresponding character using chr() function and print it using print() function.
Get Next Character in Lowercase
If we want to print next character in lowercase, we need to modify above program as shown below –
ch = input("Enter a character") ch = bytes(ch, 'utf-8') nextCh = ch[0] + 1 if(nextCh >= 122): # 122 is ASCII value of last character in lowercase nextCh = 97 # 97 is ASCII value of first character in lowercase print ("Next character is : ",end="") print(chr(nextCh))
Output:
Enter a character z Next character is : a
Using ord() and Math Calculations – 1
Using ord() and some math calculations, we can increment character and find next letter in alphabets as shown below –
ch = input("Enter a character") nextCh = ((ord(ch) - 64) % 26) + 65 print(chr(nextCh))
Output:
Enter a character A B
In above program,
- Some use facts: There are total 26 letters in alphabets and ASCII value of first character in uppercase is 65.
- ord() function provides ASCII value of any character.
If we need to find next character in lowercase, we can modify above code as shown below –
ch = input("Enter a character") nextCh = ((ord(ch) - 96) % 26) + 97 print(chr(nextCh))
Output:
Enter a character a b
In above program,
- Some used facts: There are 26 letters in alphabets and ASCII value of first character in lowercase is 97.
- ord() function provides ASCII value of any character.
Using ord() and Math Calculations – 2
There is another way to find next character in alphabets using ord() and some math calculations.
ch = input("Enter a character") nextCh = (((ord(ch) + 1) - 64) % 26) + 64 print(chr(nextCh))
Output:
Enter a character Z A
For lowercase,
ch = input("Enter a character") nextCh = (((ord(ch) + 1) - 96) % 26) + 96 print(chr(nextCh))
Output:
Enter a character a b
Using ascii_lowercase and ascii_uppercase
We can also use string.ascii_lowercase and string.ascii_uppercase to write python program to find next next character of alphabets in lowercase or uppercase respectively.
For uppercase,
import string ch = input("Enter a character") alphabets = string.ascii_uppercase chIndex = alphabets.index(ch) nextCh = alphabets[(chIndex + 1) % 26] print(nextCh)
Output:
Enter a character A B
In above program,
- ascii_uppercase returns alphabets in uppercase.
- alphabets.index(ch) returns index of entered character in list returned by ascii_uppercase
- (chIndex + 1) % 26 returns index of next character in cyclic order.
Find Next Character in Lowercase Using ascii_lowercase
We can find next character in lowercase using ascii_lowercase, like uppercase, as shown below –
import string ch = input("Enter a character") alphabets = string.ascii_lowercase chIndex = alphabets.index(ch) nextCh = alphabets[(chIndex + 1) % 26] print(nextCh)
Output:
Enter a character a b
That’s how we can write python program to find next character of alphabets with examples.
Reference: Official Doc