One
One
Complex numbers have a real part and an imaginary part denoted with j. You can
create complex numbers in Python with complex(). The first argument will be the
real part and the second argument will be the imaginary part.
>>> complex(4, 5)
(4+5j)
>>> complex(6, 8)
(6+8j)
>>> complex(0, 0)
0j
>>> complex(5)
(5+0j)
>>> complex(0, 4)
4j
Strings in Python
Strings incredibly helpful in Python. They contain a sequence of characters and
they are usually used to represent text in the code.
For example:
"Hello, World!"
'Hello, World!'
We can use both single quotes '' or double quotes "" to define a string. They are
both valid and equivalent, but you should choose one of them and use it
consistently throughout the program.
💡 Tip: Yes! You used a string when you wrote the "Hello, World!" program. Whenever
you see a value surrounded by single or double quotes in Python, that is a string.
Strings can contain any character that we can type in our keyboard, including
numbers, symbols, and other special characters.
For example:
"45678"
"[email protected]"
"#IlovePython"
💡 Tip: Spaces are also counted as characters in a string.
String: H e l l o
Index: 0 1 2 3 4
💡 Tip: Indices start from 0 and they are incremented by 1 for each character to the
right.
For example:
>>> my_string[0]
'H'
>>> my_string[1]
'e'
>>> my_string[2]
'l'
>>> my_string[3]
'l'
>>> my_string[4]
'o'
We can also use negative indices to access these characters:
>>> my_string[-1]
'o'
>>> my_string[-2]
'l'
>>> my_string[-3]
'l'
>>> my_string[-4]
'e'
>>> my_string[-5]
'H'
💡 Tip: we commonly use -1 to access the last character of a string.
String Slicing
We may also need to get a slice of a string or a subset of its characters. We can
do so with string slicing.
<string_variable>[start:stop:step]
start is the index of the first character that will be included in the slice. By
default, it's 0.
stop is the index of the last character in the slice (this character will not be
included). By default, it is the last character in the string (if we omit this
value, the last character will also be included).
step is how much we are going to add to the current index to reach the next index.
We can specify two parameters to use the default value of step, which is 1. This
will include all the characters between start and stop (not inclusive):
<string_variable>[start:stop]
For example:
>>> freecodecamp[2:8]
'eeCode'
>>> freecodecamp[0:3]
'fre'
>>> freecodecamp[0:4]
'free'
>>> freecodecamp[4:7]
'Cod'
>>> freecodecamp[4:8]
'Code'
>>> freecodecamp[8:11]
'Cam'
>>> freecodecamp[8:12]
'Camp'
>>> freecodecamp[8:13]
'Camp'
💡 Tip: Notice that if the value of a parameter goes beyond the valid range of
indices, the slice will still be presented. This is how the creators of Python
implemented this feature of string slicing.
If we customize the step, we will "jump" from one index to the next according to
this value.
For example:
>>> freecodecamp[0:9:2]
'feCdC'
>>> freecodecamp[2:10:3]
'eoC'
>>> freecodecamp[1:12:4]
'roa'
>>> freecodecamp[4:8:2]
'Cd'
>>> freecodecamp[3:9:2]
'eoe'
>>> freecodecamp[1:10:5]
'rd'
We can also use a negative step to go from right to left:
>>> freecodecamp[10:2:-1]
'maCedoCe'
>>> freecodecamp[11:4:-2]
'paeo'
>>> freecodecamp[5:2:-4]
'o'
And we can omit a parameter to use its default value. We just have to include the
corresponding colon (:) if we omit start, stop, or both:
# Default start
>>> freecodecamp[:8:2]
'feCd'
# Default stop
>>> freecodecamp[4::3]
'Cem'
f-Strings
In Python 3.6 and more recent versions, we can use a type of string called f-string
that helps us format our strings much more easily.
To define an f-string, we just add an f before the single or double quotes. Then,
within the string, we surround the variables or expressions with curly braces {}.
This replaces their value in the string when we run the program.
For example:
first_name = "Nora"
favorite_language = "Python"
print(f"Hi, I'm {first_name}. I'm learning {favorite_language}.")
The output is:
value = 5
5 multiplied by 2 is: 10