#11
CodeWithHarry
Learning about Strings
Welcome back to 100 Days of Code! Today, we will be diving
deeper into the topic of Strings. As we know, Strings are a
special Data Type which can be written by enclosing any text
in double quotes. In this blog post, we will explore other
methods of creating Strings.
Without wasting any time, let's move to the computer screen
and get started!
Opening Replit
Firstly, I will open my Repls by going to the "Zero to Hero"
Python course. But before that, I want to address something.
Many people have been mentioning me on Instagram in the
100 Days of Code challenge. I want to let you know that I will
re-mention all those who mention me in the challenge.
Understanding Strings in Python
If you want to join me on Instagram, feel free to follow me and
tag me in your stories with "Day #5 done!" or "Day #6 done!"
to boost my energy. Today, we're going to dive deep into the
topic of strings in Python.
As you may already know, a string is a data type in Python. To
create a string, all you have to do is enclose anything in
double-quotes (""). For example:
name = "Harry"
friend = "Rohan"
anotherFriend = 'Lovish'
As you can see, you can also enclose a string in single-quotes
(''). Both double-quotes and single-quotes can be used to
create strings.
, Strings Slicing and Operations on Strings in
Python | Python Tutorial - Day #12
CodeWithHarry
How to Do String Slicing in Python
Today's topic is string slicing in Python. As we learned before,
strings are a useful data type in any project. Slicing a string
means getting a substring from the original string.
Example:
Let's say we have a string named "harry" composed of 5
characters: h, a, r, r, and y. If we only want to access "rry", we
can use string slicing.
Code:
name = "harry"sliced_name =
name[2:]print(sliced_name) # Output: "rry"
If that example wasn't helpful, let's say we have a string with
multiple names separated by commas.
Code:
names = "Harry, Shubh, John, Jane, Alex"sliced_names
= names.split(", ")print(sliced_names) # Output:
["Harry", "Shubh", "John", "Jane", "Alex"]
We used the split() method to separate the names and
create a list of strings.
Let's say we have a string called "names" that contains the
word "Harry". If we want to slice out just the word "Harry" from
the string, we can use the following code:
print(names[:5])
This will print out "Harry".
If we want to slice out just the characters starting at the 5th
index, we can use the following code:
print(names[5:])