A string is a sequence of characters. Which is an array of bytes representing Unicode characters. A string character is simply a string with a length of one. The square bracket [] can be used to access elements of the string.
· Strings
in python are surrounded by either a single quotation mark or a double
quotation mark.
· Like ‘Hello python’ or “Hello python”.
print('Hello
Python')
print("Hello
Python")
Single-line string
·
To
assign a single line string to a variable, use this:
Example
a =
"Hello Python"
print(a)
Multiline string
·
To
assign the multiline string to a variable, use three single quotes or
three double-quotes.
Example
a =
'''Lorem ipsum dolor sit amet consectetur adipisicing elit. Itaque earum eius,
sapiente error quos distincti est recusandae necessitatibus provident'''
print(a)
Example
a =
"""Lorem ipsum dolor sit amet consectetur adipisicing elit.
Itaque earum eius, sapiente error quos distincti est recusandae necessitatibus
provident"""
print(a)
Note:-
The line breaks are inserted at the same position.
Looping a string
·
We
can loop through the characters in a string with a “for” loop.
Example
for
x in "Hello":
print(x)
String length
·
The
len() function returns the length of string.
Example
a =
"Hello Python"
print(len(a))
Good
ReplyDeleteThanks for the feedback.
DeleteNice article
ReplyDelete