Python string is the collection of the characters surrounded by single quotes, double quotes, or triple quotes. The computer does not understand the characters; internally, it stores manipulated character as the combination of the 0's and 1's.
Creating string in python
#Using single quotes
str1 = 'Hello Python'
print(str1)
#Using double quotes
str2 = "Hello Python"
print(str2)
#Using triple quotes
str3 = '''''Triple quotes are generally used for
represent the multiline or
docstring'''
print(str3)
o/p:
Hello Python
Hello Python
''Triple quotes are generally used for
represent the multiline or
docstring
==================================================================
#assigning string to a variable
print("------------#assigning string to a variable---------")
a="hello"
print(" a value is:", a)
print(" ")
print("============================")
print("============================")
o/p:
hello
==================================================================
#multiple line strings
print("------------#multiple line strings---------")
print("------------#multiple line strings---------")
a="""Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua"""
print("a is:", a)
print(" ")
o/p:
a is: Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua
==================================================================
#strings are arrays
print("------------#strings are arrays---------")
print("------------#strings are arrays---------")
a="hello world"
print("a value is:",a)
print(" ")
print("a[0] value is :", a[0])
print(" ")
print("a[1] value is :", a[1])
print(" ")
o/p:
a value is: hello world
a[0] value is : h
a[1] value is : e
==================================================================
#looping through a string
print("------------#looping through a string---------")
for x in "haritaraka":
print(x)
print(" ")
print("============================")
print("------------#looping through a string---------")
for x in "haritaraka":
print(x)
print(" ")
print("============================")
o/p:
h
a
r
i
t
a
r
a
k
a
==================================================================
#string length
print("------------#string length---------")
a="hello,world"
print("lenght of a is:",len(a))
print(" ")
print("============================")
print("------------#string length---------")
a="hello,world"
print("lenght of a is:",len(a))
print(" ")
print("============================")
o/p:
lenght of a is: 11
==================================================================
#check string
print("------------#check string with if condition--------")
txt="hari taraka prabhu"
print("txt value is :", txt)
print(" ")
print("free" in txt)
print(" ")
print("============================")
o/p:
txt value is : hari taraka prabhu
False
==================================================================
#check string with if condition
print("------------#check string with if condition---------")
txt="the best things in life are free"
print("txt value is :", txt)
print(" ")
if "free" in txt:
print("free" in txt)
print(" ")
print("============================")
o/p:
txt value is : the best things in life are free
True
==================================================================
#check string with if not condition
print("------------#check string with if not condition---------")
txt="the best things in life are free"
print("txt value is :", txt)
print(" ")
txt = "The best things in life are free!"
print("expensive" not in txt)
print(" ")
print("============================")
o/p:
txt value is : the best things in life are free
True