String

  • Strings in python are surrounded by either single quotation marks, or double quotation marks.
  • Even triple quotes can be used in Python but generally used to represent multiline strings.
  • Python does not have a character data type, a single character is simply a string with a length of 1. 

Create with Single quotes

String1 = 'Welcome to the Reviving India'
print(String1)
print(type(String1))

Double quotes

String1 = “Welcome to the Reviving India”
print(String1)
print(type(String1))

Triple quotes

String1 = '''Python Tutorial'''
String2 = """ Hello world"""
print(String1,String2)
print(type(String1),type(String2))

Read or Access String

  • String can be accessed by using the method of Indexing.
  • Indexing allows both positive & negative address references to access characters
  • 0 refers Ist character from the beginning and so on
  • -1 refers to the last character, -2 refers to the second last character and so on.
    String1 = 'Welcome to the Reviving India'


    print(String1)   #Complete String


    print(String1[0]) #Ist substring

    print(String1[-1]) #Last substring

    print(String1[0:5]) #Mutiple substring

    Update

    • Updating of substring from a String is not allowed.
    • This will cause an error because item assignment from a String is not supported

    Insert

    • Inserting of substring from a String is not allowed.
    • This will cause an error because item assignment from a String is not supported.
    • But we can achieve this through string concatenation (c = a+b)

    Delete

    • Deletion of entire string is possible with the use of del keyword but deletion of characters from a String is not allowed
    • del <string_name>

    0 Comments

    Leave a Reply

    Your email address will not be published. Required fields are marked *