String Conversion

String Conversion

str(), int(), and repr()

Were you expecting 2? Were you expecting '2'? Were you expecting '11'? Python doesn't know which one you want...a string...or an integer. To bypass, you need to either convert the '1' to an int 1, or convert the int 1 to a string '1'. Converting both to strings, however will get you the string '11' Converting both to integers will get you 2. The built in functions str() will convert to a string and the int() will convert to an integer.

The built in function repr() will convert to as-code string.

Character Code Conversions

ord() and chr()

Every character on a computer has an ASCII integer code. The built in functions ord() will convert the single character to its ASCII integer, while chr() will do the opposite and convert the ASCII integer to it's single character.

So If you want to progress to the next character via ASCII...

String Changing

Because strings are an immutable sequence we cannot change the index of a string by reassigning a new index, like so:

To do this we can use a slice to change and reassign it back to the same varaible.

There are better ways to change a string than to slice it sometimes with the built in string methods. Check the tab for String Methods