More Functionality
#Create a long string variable
text = "Technology is nothing. What's important is that you have a faith in people, that they're basically good and smart, and if you give them tools, they'll do wonderful things with them."
The len function returns the length of a string (as well as certain other variables).
#len() returns the length of something
#In this case our string has 181 characters
print(len(text))
The string from before is way too long for one line, which is bad practice. For these cases, you can use triple quotes and multiple lines. These are called block quotes and are useful when you need multiple lines.
#Block quotes let us write out a string on multiple lines
text = """Technology is nothing. What's important is that you have a faith in people,
that they're basically good and smart, and if you give them tools, they'll do wonderful things with them."""
print(text)
The lower() function which is called on the string object puts every single character into lowercase.
#lower() will lower the text for us
print(text.lower())
It is important to note that this does NOT modify it in place. If we print the string again it is still in its prior format.
#It is not modified in place!
print(text)
#So if we want it to be permanent then we would say text equals the lowercased version
text = text.lower()
print(text)
The upper function puts everything into uppercase.
#Upper makes everything uppercased
print(text.upper())
#Bring back the original
text = """Technology is nothing. What's important is that you have a faith in people,
that they're basically good and smart, and if you give them tools, they'll do wonderful things with them."""
The find() function returns the index of a string within the string it is called on. Below, if we want to find the string is, we call it like below.
#Find returns to us the index of a word we give it
print(text.find("is"))
This is useful if, for example, we wanted all text after the word is.
#Get the index
i = text.find("is")
#Get all text after
print(text[i:])
This function will only find the index of the first instance unless we pass a second argument which is the index to start looking after. If we pass 12 then we look for the word is after the index of 12.
#We find that the next instance is at 40
print(text.find("is",12))
#Get the index
i = text.find("is",12)
#Get all text after
print(text[i:])
If the string is not in there then -1 will be returned.
print(text.find("sleep"))
A final function to look at is split. What this does is find all instances of a string and then splits the string up into pieces where that string is present. We have not learned about lists yet, so just take this as a sneak peak.
#The split function returns the string split into pieces
print(text.split('faith'))
Floating Point Numbers¶
Floating point numbers are similar to integers except that they have decimal components. Integers on the other hand are only whole numbers.
#Create a number with a decimal
a = 2.5
#Print the number
print(a)
#The type is a float
print(type(a))
If we want to quickly set multiple variables to the same value we can have multiple equal signs and then all the variables to the left are set equal to the variable on the right.
#The way to define two variables with the same values
a = b = 5
print(a)
print(b)
To set two variables equal to different things at the same time you can use one equal sign and set multiple variables separated by commas to multiple values separated by values.
a, b = 10, 100
print(a)
print(b)