Hello everyone and welcome to this new python tutorial !!!
Today’s videos is about some things that I found really interesting in controlling strings!!
The first set method that we goanna check out is of knowing if the provided string character is a number or letter !
The second set of methods is to convert alphabetic characters to numeric code and vice versa.
To explain this or to put is application, we will try to build a counter that understands if the input is a letter or number and then and continue the count in this criteria
So without futher due lets dive in;
Out first methods are the “isdigit()” and “isalpha()”
And now that we understood how to differentiate between letters and numbers
Lets create the counter!!!
- For the numbers there is no problems, we ll just create a loop
- For the second part where the input is a letter,,,, there where it gets tricky
First we will use the ord() function to get the character code
And now that we got a number there is no problem
Lets create a normal loop and within this loop we will convert the number back to a letter
One final step is to insure that when the counter reaches “Z” it will restart back to “A”
Source Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
start_str= input("enter a number or a letter to start counting \n") steps=int(input("enter number of steps \n ")) if start_str.isdigit(): counter=int(start_str) for c in (range(counter, counter+steps)): print(str(counter)) counter+=1 if start_str.isalpha(): counter=ord(start_str) for l in range(counter, counter+steps): print(chr(counter) + " is " + str(counter )) counter+=1 if counter == 91: counter=65 elif counter ==123: counter=97 |