Python has a built-in string class named "str" with many handy features (there is an older module named "string" which you should not use).
String literals can be enclosed by either triple or double or single quotes, although single quotes are more commonly used.
Backslash escapes work the usual way within both single and double quoted literals -- e.g. \n \' \". A double quoted string literal can contain single quotes without any fuss (e.g. "I didn't do it") and likewise single quoted string can contain double quotes. A string literal can span multiple lines, but there must be a backslash \ at the end of each line to escape the newline.
String literals inside triple quotes, """" or ''', can span multiple lines of text.
Example:
#Display string literal
print('Silan Software')
print("Silan Software")
print('''Silan Software''')
Output:
Silan Software
Silan Software
Silan Software
#Assign a string value to a variable
s1='Python Programming'
print(s1)
Output:
Python Programming
s2="Python for AI"
s2
Output:
Python for AI
s3='''Python for Data Science'''
s3
Output:
Python for Data Science
#Multiline strings
a="""Python is a programming
language,
Python is developed by Guido
Van Roosum"""
print(a)
Output:
Python is a programming
language,
Python is developed by Guido
Van Roosum
type(a)
Output:
str
a="Hello, World!"
print(a[1])
Output:
e
#Loop through a string
for x in "apple":
print(x)
Output:
a
p
p
l
e
a="Hello, World!"
print(len(a))
Output:
13
#check string
str="The best things in life is education"
print("life" in str)
Output:
True
#Slicing
b="Hello, World!"
print(b[2:5])
Output:
llo
b="Hello, World!"
print(b[:5])
Output:
Hello
b="Hello, World!"
print(b[2:])
Output:
llo, World!
c="Hello, World!"
print(c[-5:-2])
Output:
orl
a = "Hello, World!"
print(a.upper())
Output:
HELLO, WORLD!
a = "Hello, World!"
print(a.lower())
Output:
hello, world!
a = "Hello, World!"
print(a.replace("He", "Jf"))
Output:
Jfllo, World!
#strip() : removing white space from the begining or the end
a=" Hello, World! "
print(a.strip())
Output:
Hello, World!
a = "Hello, Wor,ld!"
print(a.split(",")) # returns ['Hello', ' World!']
Output:
['Hello', ' Wor', 'ld!']
#Concatnating two strings
a = "Hello"
b = "World"
c = a + b
print(c)
Output:
HelloWorld
a = "Hello"
b = "World"
c = a + " " + b
print(c)
Output:
Hello World
#Python program to Count occurences of a character in a string
count = 0
for k in 'Hello World':
if k == 'l':
count += 1
print("the occurrences of l is",count)
Output:
the occurrences of l is 3
# Python program to find the reverse a given String Using function
def reverse(str): #function definition
rev = ""
for k in str:
rev = k + rev
return rev
# given string
mystr = input("enter the string value")
# reversed string
print("Reversed String: ", reverse(mystr))
Output:
enter the string valuepython
Reversed String: nohtyp
# Python program to find the reverse a String and check whether given string is palindrome
def reverse(str): #function definition
rev = ""
for k in str:
rev = k + rev
return rev
# given string
mystr = input("enter the string value")
# reversed string
rev=reverse(mystr)
#Check string is palindrome or not
if mystr==rev:
print("string is palindrome")
else:
print("string is not palindrome")
Output:
enter the string valuekatak
s
string is palindrome
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc