Python Strings

 

Python string is the collection of the characters surrounded by single quotes, double quotes, or triple quotes.

#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)

String Indexing

# Given String
str = "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])

str = 'PYTHONSTRING'
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)


  1. str = "HELLO"
    print(str)
    str = "hello"
    print(str) 

Deleting the String

str = "HELLO"
del str[1]

str1 = "HELLO"
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 watch this video and let me know in the comments section if you have any questions.

Please do like, share and subscribe my youtube channel for more such videos.

https://youtu.be/YxpEpHAg72A

Comments

Popular posts from this blog

How to Run Python in MS Visual Studio Code

Python Loop Statements