0% found this document useful (0 votes)
23 views5 pages

One

The document discusses strings in Python. It explains that strings contain sequences of characters and can be defined using single or double quotes. It also covers string indexing, slicing, formatting with f-strings and other string methods.

Uploaded by

akhilkadali123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views5 pages

One

The document discusses strings in Python. It explains that strings contain sequences of characters and can be defined using single or double quotes. It also covers string indexing, slicing, formatting with f-strings and other string methods.

Uploaded by

akhilkadali123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Complex

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.

These are some examples:

>>> complex(4, 5)
(4+5j)

>>> complex(6, 8)
(6+8j)

>>> complex(3.4, 3.4)


(3.4+3.4j)

>>> 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.

Quotes Within Strings


If we define a string with double quotes "", then we can use single quotes within
the string. For example:

"I'm 20 years old"


If we define a string with single quotes '', then we can use double quotes within
the string. For example:

'My favorite book is "Sense and Sensibility"'


String Indexing
We can use indices to access the characters of a string in our Python program. An
index is an integer that represents a specific position in the string. They are
associated to the character at that position.

This is a diagram of the string "Hello":

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 = "Hello"

>>> 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 = "Hello"

>>> 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.

This is the general syntax:

<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 = "freeCodeCamp"

>>> 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 = "freeCodeCamp"

>>> 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 = "freeCodeCamp"

>>> 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:

>>> freecodecamp = "freeCodeCamp"

# Default start and step


>>> freecodecamp[:8]
'freeCode'

# Default end and step


>>> freecodecamp[4:]
'CodeCamp'

# Default start
>>> freecodecamp[:8:2]
'feCd'

# Default stop
>>> freecodecamp[4::3]
'Cem'

# Default start and stop


>>> freecodecamp[::-2]
'paeoer'

# Default start and stop


>>> freecodecamp[::-1]
'pmaCedoCeerf'
💡 Tip: The last example is one of the most common ways to reverse a string.

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:

Hi, I'm Nora. I'm learning Python.


Here we have an example where we calculate the value of an expression and replace
the result in the string:

value = 5

print(f"{value} multiplied by 2 is: {value * 2}")


The values are replaced in the output:

5 multiplied by 2 is: 10

You might also like