Python 07 - String Manipulation

String - a sequence of characters.

This definition implies that the characters in a string are in order and that they each have a position or index.

String indexing begins at zero, so the first character in the string has an index of 0, the second has an index of 1, and so on.

So a string that is n characters long will have indexes 0 through n-1

Square brackets [] are used next to the string, and the index of the character you want, goes next to the string.

Getting One Character [index]

Index 0 is always the first character

Index 0 is always the first character

Index -1 represents the last character of any string

Index -1 represents the last character of any string

Getting Multiple Characters [start:stop]

To get multiple consecutive characters, you can put two numbers in the brackets.

The first number is where you want to start, and the second number is where you want to stop.

***The stop number is EXCLUSIVE, so you won’t get that character, you’ll only get up the character right before it.

if no start index is specified, index 0 is implied

if no start index is specified, index 0 is implied

if no stop index is specified, Python will just go to the end of the string

if no stop index is specified, Python will just go to the end of the string

no start index, index -1 represents the last character, so from the beginning up to (excluding) the last character

no start index, index -1 represents the last character, so from the beginning up to (excluding) the last character

Getting Multiple Characters [start:stop:step]

To get multiple non-consecutive characters, you can put a third number in the brackets.

The third number is for step.

a step of 1 would be every single character, so a step of 2 means every other charcter

a step of 1 would be every single character, so a step of 2 means every other character

a step of -1, negative means backwards and 1 is every character, so every character, but backwards

a step of -1, negative means backwards and 1 is every character, so every character, but backwards

The len() Function

takes an object as an argument, returns the length or number of items in that object

Screen Shot 2021-03-03 at 5.13.06 PM.png




Previous
Previous

04 - Objects, Type Casting