Introduction
Welcome to my 11th blog post. On day 11, I learnt about strings in python and a few important characteristics concerning strings. I learnt about strings, multi-line strings, accessing string characters and looping through the strings. Let's dive deep into the details
So let's get started......
What is a String?
Anything that you enclose within single quotes ('..') or double quotes ("..") is considered a string in python. A string is essentially a sequence or array of characters. A string is like an array of characters, not exactly array.
Strings are used when working with Unicode characters.
Example -
name = "Harry"
print("Hello, " + name)
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. For example, consider the sentence: He said, βI want to eatβ.
How will you print this statement in python?: He said, "I want to eat".
We will use single quotes for our convenience in this case
print('He said, "I want to eat an apple".')
Important Note -
We cannot use "........." inside of "........". Either we use escape sequence charcters like \" and use " " or we need to enclose double quotes ("") inside of single quotes
('..... " ..... " ..')
Multiline strings
To write multi-line strings we make use of triple quotes like ''' ...... '''
Example -
a = """Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua."""
print(a)
Accessing characters of a string
In Python, the string is like an array of characters, not exactly. We can access parts of a string by using its index which starts from 0. Square brackets [ ..... ]can be used to access elements of the string.
Example -
print(name[0])
print(name[1])
Looping through strings
We can use for loop to traverse through the string elements
Example -
for character in name:
print(character)
Above print statement will print all the characters in the name one by one.
Strings - Example
Code -
name = "Harry"
friend = "Rohan"
anotherFriend = 'Divya'
apple = '''He said,
Hi Harry
hey I am good
"I want to eat an apple'''
print("Hello, " + name)
#print(apple)
print(name[0])
print(name[1])
print(name[2])
print(name[3])
print(name[4])
print(name[5]) # Throws an error
print("Lets use a for loop\n")
for character in anotherFriend:
print(character)
output -
output -
Hello, Harry
H
a
r
r
y
Lets use a for loop
D
i
v
y
a
Resources Used
You can watch the video of Day#11 by clicking on the below link πππππ
Conclusion
Thanks, guys for going through this blog post. This was the introduction to the world of strings and some syntax that is used to write strings in python and a few important pointers that one should bear in mind.
Thank you if you read this post and have found this post useful of my learnings from Day #11 of python
See you in the next one.....
About Me
Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practising coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.
I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.
So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.