Strings in Python
Strings:
A string is a data type in Python. A string is a sequence of characters enclosed in quotes. If we have to write a string, we can use it in these three ways.
a = 'hammad' ➡️ Single quoted string
a = "hammad" ➡️ Double quoted string
a = '''hammad''' ➡️ Triple quoted string
String Slicing:
A string in Python can be sliced to get a part of the string.
Let's say we write a program like name = "hammad". Now, if we have to slice the string, we use .len()
"hammad"
Positve index = 0123456
Negative index = -6 -5-4-3-2-1
Note:
Positive index are start from 0 (Left to right)
Negative index are start from -1 (Right to left)
The index in a string starts from 0 to (length -1) in Python. In order to slice a string, we use the following syntex.
s = name[start : end]
first index is included, and the last index is not included.
Escape Sequence Characters:
Sequence of characters after backslash "\" are called Escape Sequence characters.
These characters represent one special character inside strings.
For example:
\n for new line
\t for tab space
\\ for backslash
\' for single quote
etc
6
3 comments
Hammad Mansoor
5
Strings in Python
powered by
Make $1k-$10k in 30 days
skool.com/make-1k-5k-in-30-days-8449
Media Valley School is the fastest way to hit $1K/month with freelancing or agency work. Guaranteed.
Land your first $1K month in 30 days
Build your own community
Bring people together around your passion and get paid.
Powered by