11 - Strings

🧢 Tags:: #Python_Basics
Up:: 12 - String Slicing & Operations on String
Down:: 10 - Taking User Input
πŸ—ƒ Resources:: Playlist
2023-12-10 - 20:15
In python, anything that you enclose between single or double quotation marks is considered a string.

A string is essentially a sequence or array of textual data. Strings are used when working with Unicode characters.


name = "Exia"
print("Hello, " + name)


Hello, Harry

Note: It does not matter whether you enclose your strings in single or double quotes, the output remains the same.

Sometimes, the user might need to put quotation marks in between the strings. Example, consider the sentence: He said, β€œI want to eat an apple”.

How will you print this statement in python?:Β He said, "I want to eat an apple".Β We will definitely use single quotes for our convenience

print('He said, "I want to eat an apple".')

Multiline Strings

If our string has multiple lines, we can create them like this:

a = """Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua."""

Accessing Characters of a String

In Python, string is like an array of characters. We can access parts of string by using its index which starts from 0.
Square brackets can be used to access elements of the string.


Looping through the string

We can loop through strings using a for loop like this:

for character in name:

Above code prints all the characters in the string name one by one!