-
Integers, Floats and Strings 3
-
Lecture1.1
-
Lecture1.2
-
Lecture1.3
-
-
If Statements 3
-
Lecture2.1
-
Lecture2.2
-
Lecture2.3
-
-
Lists, Sets and Tuples 5
-
Lecture3.1
-
Lecture3.2
-
Lecture3.3
-
Lecture3.4
-
Lecture3.5
-
-
Loops 3
-
Lecture4.1
-
Lecture4.2
-
Lecture4.3
-
-
Functions 3
-
Lecture5.1
-
Lecture5.2
-
Lecture5.3
-
-
Dictionaries 3
-
Lecture6.1
-
Lecture6.2
-
Lecture6.3
-
-
Assertions 2
-
Lecture7.1
-
Lecture7.2
-
-
Classes 3
-
Lecture8.1
-
Lecture8.2
-
Lecture8.3
-
-
Matplotlib 2
-
Lecture9.1
-
Lecture9.2
-
String Functionality
fstring = "-- {} --".format(500)
print(fstring)
-- 500 --
You can pass more than one variable.
fstring = "-- {} {} --".format(500, "ABC")
print(fstring)
-- 500 ABC --
Note that you can't add a string and an integer together normally. The code below will break because they are two different types of variables.
#This will break
print("A"+1)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-18-40c4350b5c6f> in <module>
1 #This will break
----> 2 print("A"+1)
TypeError: can only concatenate str (not "int") to str
You can, however, convert an integer to a string using the function str(). This will change the type to string.
#You can add a string and a number by converting
print("A"+str(1))
A1
Likewise, a string could be converted to an integer by calling int() on a string. This will only work if the string could be a valid integer!
#int() converts a variable to an integer
int("1")+2
3
String Indexing¶
In python, as well as other computer programming languages, we use indices to keep track of different pieces of data. These indices in python begin at 0 as the first element, followed by 1, 2, etc. By passing [i] where i is an index number, we get back the value at that index. For example to get the first letter of the string in "Hello World", we need to pass [0].
#Create the variable
s = "Hello World"
#Grab the first character, which is the letter H
print(s[0])
H
Likewise, the second element is found by calling [1].
#Get the second element
print(s[1])
e
You can also get a range of elements by calling [i1:i2] where i1 is the first element to include, and all elements from i1 to i2 - 1 are included. So i1:i2 means all elements from i1 to i2 excluding i2. In the following example, [0:2] returns the first and second characters.
#Get the first and second characters
print(s[0:2])
He
#Here i1=0 and i2=1, so we get only the first element
print(s[0:1])
H
#Grab the second and third element
print(s[1:3])
el
If you leave out i1, it means grab all elements to the left of i2. It is the same as saying i1=0.
#Leaving out the left index gives us everything up to but not including the right index
print(s[:3])
Hel
Likewise, leaving out i2 gives everything to the right of i1 including i1.
#Leaving out the right index gives us everything from the left index on
print(s[3:])
lo World
If, instead of finding the largest index and subtracting, we wanted to have a simple way to say the last, second to last etc. index, we would use negative indexing. To get everything except the last element, we could use -1.
print(s[:-1])
Hello Worl
Likewise, -2 stands for the second to last element.
print(s[:-2])
Hello Wor
If we wanted to get the last five letters of a string:
print(s[-5:])
World
Finding the length of a string¶
The len() function will return back the length of a string.