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.
Traceback (most recent call last): File "/home/fabc221bf999b96195c763bf3c03ddca.py", line 9, in s = s + 1 TypeError: cannot concatenate 'str' and 'int' objects
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.
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.
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Ways to remove i'th character from string in Python
- Increment and Decrement Operators in Python
- Python | Different ways to kill a Thread
- Different ways to clear a list in Python
- Python | Ways to remove a key from dictionary
- Python | Ways to shuffle a list
- Print lists in Python (4 Different Ways)
- Reverse string in Python (5 different ways)
- Python | Ways to Copy Dictionary
- Python | Ways to Spilt the list by some value
- Python | Ways to concatenate two lists
- Extending a list in Python (5 different ways)
- Python | Ways to flatten a 2D list
- Python | Ways to rotate a list
- Zip function in Python to change to a new character set