Now we are going to look at using our new knowledge about control structures to do some more things with strings. 

Things You Can Do With Strings

Length of a String:  len()

l

Getting a Single Character

Try the following in the IDLE shell:

You can get any single character from a string by using a square bracket around an index.  Note that indexing of a string starts from 0.

Getting a part (slice) of a string

When thinking about string indexes for slices of strings, it's easiest to think of the string in numbered cells like in this diagram:

Try out the following to get parts of the string:

Notice how when you want the beginning of a string, you don't need to type the 0 index, but can just put [:5] and when you want the end of the string, you don't need to put the last index  [5:]

If you want an exact copy of your string, you can use the following:

Here are some other things you can do:

Multiply sections of string:

 Change to upper case

Change to lower case

Replace parts of the string

Find the first and last letter alphabetically in a string

 
Strings are not mutable.  This means that you can't change the values within a string.  It seems like you can, but all you can do is rewrite the whole string with a new value.  For example the replace method we used above actually makes a new string. 

Words without Vowels (and more for loops)

Twyndyllyngs is an obsolete English word that doesn't contain any vowels (aeiou).  There are a few others, but not too many.  We are going to use Python to remove vowels from sentences we type in to see if we can still read them.

Type in the example below, save it and run it, and try to understand how it works.  Take careful note of how the for loop is set up.

These lines get the input from the user, then initialise the variables. 
This for loop will take each item in the string one at a time and store it in a variable called letter.
This checks that the letter is not a vowel, and then if it isn't a vowel it rewrites new_message with the letter on the end.
 
 
It doesn't matter what you name the variables, the instructions will still work.  It's just that giving them sensible names makes your code easier to understand.  The following code would work just as well:

 

Incrementing More Easily

Instead of putting:

new_message=new_message + letter

(Remember to read it as new_message becomes new_message + letter) you can write it as

new_message+=letter