Thursday, October 28, 2021

Strings

A string consists of characters inside 'single quotes' or "double quotes". It can be any length and can contain letters, numbers, symbols, and spaces!!

A string can be considered a "list of characters".


Slicing a string: 

What a helpful diagram this is! 


If you want the letters b and l:
        print(favorite_fruit[0:2])

It works like a number line - from 0 to 2 inclusive. It doesn't include any numbers from 2.01 and up, so the only numbers inside this slice are b and l.

Open ended selections work like this:
        print(favorite_fruit[:4])  #this will print everything from 0 up to 4
        output: blue

        print(favorite_fruit[4:]) #this will print everything from 4 to the end of the string
        output: berry


Slicing with a function:



Printing the last character of a string using len():
        You write the following code:
            last_character = favorite_fruit[len(favorite_fruit)]
            print(last_character)

And you get an error... why? It's because the length of the string is 8 characters, and with a zero index it's 9. There's no character in the 9 position. So what do you do?
        last_character = favorite_fruit[len(favorite_fruit)-1]
        print(last_character)
        
        Output will be y (for the word blueberry)

You can also slice the last few characters like this:
        length = len(favorite_fruit)
        last_characters = favorite_fruit[length-4:]
        print(last_characters)

        Output will be erry (the last 4 characters from blueberry)

It took many iterations for this to work out. It's ok, I think I get it now:


First, define the function to accept 2 items. 
        def password_generator(first_name, last_name):

Then assign a variable to accept the thing that you want: the last 3 letters of the first name and last name using the slicing method we learned just above:
        temp_password = first_name[len(first_name)-3:] + last_name[len(last_name)-3:]

Then return whatever password this generates:
        return temp_password

Then call it to use on the first_name and last_name entered above:
        temp_password = password_generator(first_name, last_name)

Using negative indices to slice


The exercise for negative indices:



Strings are immutable

"Immutable" means that it cannot change once created. "Mutable" data types can change, and "immutable cannot be changed. 

Example of trying to fix a string but it won't work: (Change name from Bob to Rob)


Have to do it a different way:


Escape characters

Escape characters are ones that have some kind of special meaning in python. If you accidentally end a string before including everything you need to, you can use the backslash to include it again. Here is an example:

        fruit = "My favorite fruit" is blueberry ""

This can happen accidentally sometimes, so you add a backslash to the parts you want included in the string:
    
        fruit = "My favorite fruit\" is blueberry\""

Still need some practice on this one:


This didn't work the first time because I forgot to return the counter! 


Other things we can do with strings:



Two exercises with commentary:




No comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Strings

A string consists of characters inside 'single quotes' or "double quotes". It can be any length and can contain letters, ...