Python Strings
Python string is the collection of the characters surrounded by single quotes, double quotes, or triple quotes.
#Using single quotesstr1 = '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)
String Indexing
# Given Stringstr = "PYTHONSTRING"
# Start Oth index to end
print(str[0:])
# Starts 1th index to 4th index
print(str[1:5])
# Starts 2nd index to 3rd index
print(str[2:4])
# Starts 0th to 2nd index
print(str[:3])
#Starts 4th to 6th index
print(str[4:7])
print(str[-1])
print(str[-3])
print(str[-2:])
print(str[-4:-1])
print(str[-7:-2])
# Reversing the given string
print(str[::-1])
print(str[-12])
Reassigning Strings
str = "HELLO"str[0] = "h"
print(str)
- str = "HELLO"
print(str)
str = "hello"
print(str)
Deleting the String
str = "HELLO"del str[1]
del str1
print(str1)
String Operators
str = "Hello"str1 = " world"
print(str*3) # prints HelloHelloHello
print(str+str1)# prints Hello world
print(str[4]) # prints o
print(str[2:4]); # prints ll
print('w' in str) # prints false as w is not present in str
print('wo' not in str1) # prints false as wo is present in str1.
print(r'C://python37') # prints C://python37 as it is written
print("The string str : %s"%(str)) # prints The string str : Hello
Please do like, share and subscribe my youtube channel for more such videos.
https://youtu.be/YxpEpHAg72A
Comments
Post a Comment
Please comment