You are on page 1of 28

Strings in Python

Instructor: Rosalie Buena, MM, BSCS


Strings in Python

A string can be seen as a sequence of


individual characters.
Strings Python
Strings can be created using
1. single quotation marks (') ' This is a string with simple quotes’
2. double quotes (") "Mayers' Dachshund is called Waldi"
3. triple quotes (''') or (""")
''' String in triple quotes can also span multiple lines and contain 'single'
and 'double' quotes. ''' can be specified
Strings in Python

Output:
Important String Functions Python
A couple of string functions:
1. CONCATENATION This function is used to connect two strings to a
new string using the "+" operator:

"Hello" + "World" ->"HelloWorld"


String Concatenation in Python

This joins/concatenates the 2 strings together

Output:
String Concatenation in Python

Output:
String Concatenation in Python

Output:
String Concatenation in Python

We removed the space


Output:
Important String Functions Python
A couple of string functions:
2. REPETITION A string can be repeated concatenated. To do this, use
the "*" operator. Example:

"* - *" * 3 becomes "* - ** - ** - *"


Important String Functions Python
2. REPETITION
Important String Functions Python
A couple of string functions:
3. INDEXING

"Python" [0] ->"P"


Strings Python

Every single character of a string can be


addressed via an index.
Strings Python
The indices are also numbered from the right by negative indices, ie the
last character is addressed by means of the index -1, the penultimate
character by means of -2 etc. We see this illustrated in the following
figure:
Important String Functions Python
A couple of string functions:
4. SLICING
You cut a "slice" out of a string. In the following expression, [2: 4]
means that we cut out a substring from the string "Python", which
begins with the character of index 2 (inclusive) and goes up to index 4
(exclusive):

"Python" [2: 4 ] ->"th"


Start Stop
value value
Important String Functions Python
4. SLICING

Output:
Important String Functions Python
4. SLICING

Output:
Important String Functions Python
4. SLICING

Write a code that gets the slice from position 0,


up to but not including position 9. What will be
the output?
Output:
Important String Functions Python
4. SLICING

What if you don’t provide a start value?


You’ll still need the colon.
Output:
Important String Functions Python
4. SLICING A SEQUENCE
Important String Functions Python
4. SLICING A SEQUENCE
Important String Functions Python
4. SLICING A SEQUENCE
Important String Functions Python
4. SLICING A SEQUENCE

Output:
Important String Functions Python
4. SLICING A SEQUENCE

Using slicing, write a code that if we run it, the output is Norwegian blue.
Output:
Important String Functions Python
4. SLICING A SEQUENCE

Assignment:
Important String Functions Python
A couple of string functions:
5. LENGTH OF A STRING

len ("Python") -> 6


Immutable Strings Python
As in Java but not as in C or C ++, strings cannot be changed in Python.
If you try to change an indexed position, an error message is generated:
Strings Python
The length of a string can be determined with the len () function and
you can also easily access the last or penultimate character of a string,
for example: length of a string

You might also like