Home Schedule
        

Examples of using stringh and list functions.

# Strings have some special built-in functions
# We call these fuctions using a . after the
#   string or name of a varible that holds
#   a string

# Converting a string to all lower case with the
#   string lower function
print("Call lower function on a string literal")
print("UPPER CASE".lower())

print("\nCall lower function on a string variable")
string_var = "Run JUMP Swim!"
print(string_var.lower())


# Converting a string to all upper case with the
#   string upper function 
print("\nCall upper function on a string")
print(string_var.upper())


# Splitting a string into a list of words
print("\nSplit a string into a list of words")
print(string_var.split())

print("\nSplit a string on a specific character (U)")
print(string_var.split('U'))

# For loops can work on lists
print("\nFor loops and lists")
string_list_data = string_var.split()
for item in string_list_data:
    print(item + ": " +  str(len(item)))

    
# You can access specific indexes in a list
print("\nAccess an item in a list with an index")
print(string_list_data[1])


# Using range, for, and a list
print("\nAccess items in a list using for and range")
for index in range(len(string_list_data)):
    print(str(index) + ": " +  string_list_data[index])

# if statements can also search lists to check for items 
print("\nif statement with a list")
if 'swim!' in string_var.lower().split():
    print("Found!")
else:
    print("Not Found")

    
# You can also get the index of an item in a list
#   CAUTION: If the item isn't in the list the code will error.
#   If you want to simply know if something is in the list, use
#   if and in (like above).
print("\nFind index of item in list")
index = string_list_data.index("JUMP")
print(index)

# Special characters
print("\nUse newline and tab in a string")
print("Hello\tthere\nNice to meet you!")