Python Strings

Python String is a built-in data type representing the arrays of bytes representing the Unicode characters. Python does not have the character data type, and a single character is simply a string with a length of 1.

How to create a string in Python

To create a string in Python, you can use “single quotes(”),” or “double quotes(“”)”, or even triple quotes(”’ ”’).

Example

print("Millie Bobby Brown")
print('Millie Bobby Brown')
print('''Millie Bobby Brown''')

Output

Millie Bobby Brown
Millie Bobby Brown
Millie Bobby Brown

We can display the String literal with the print() function.

We used single, double, and triple quotes in the above example. We will get the same output, but the triple quote is helpful when we have Double quoted words as a string.

print('''Mill's ''')

Output

Mill's

How to access characters in Python

To access the characters of Python, you can use an “index” like arrays. For example, individual characters of the String can be accessed by using a method of Indexing. The slicing method is used to access the range of characters in the String.

Slicing in the String is done using the Slicing operator (colon). Indexing allows negative address references to access the characters from the back of a String, e.g., -1 refers to the last character, -2 refers to a second last character, and so on.

While accessing the index out of the range will cause the IndexError. Additionally, only Integer datatypes can be passed as the index, float, or other types will cause a TypeError.

Example

StrA = "MillieBobbyBrown"

print("Initial String: ", StrA)
print("\nFirst character of String is: ", StrA[0])
print("\nLast character of String is: ", StrA[-1])
print("\nSlicing characters from 3-12: ", StrA[3:12])
print("\nSlicing characters between " + "3rd and 2nd last character: ", StrA[0:-5])

Output

Initial String: MillieBobbyBrown

First character of String is: M

Last character of String is: n

Slicing characters from 3-12: lieBobbyB

Slicing characters between 3rd and 2nd last character: MillieBobby

How to delete characters from a String in Python

In Python, updation or deleting the characters from the String is prohibited. It will cause an error because item assignment or item deletion from the String is not supported.

Although deletion of the entire String is possible with the built-in del keyword, it is because Strings are immutable. Hence elements of the String cannot be changed once assigned.

Example

StrA = "MillieBobbyBrown"
print("Initial String: ") 
print(StrA)

StrA[2] = 'K'
print("\nUpdating character at 2nd Index: ") 
print(StrA)

Output

TypeError: 'str' object does not support item assignment

Delete the String using the del keyword.

StrA = "MillieBobbyBrown"
print("Initial String: ") 
print(StrA)

del StrA
print(StrA)

Output

Initial String:
MillieBobbyBrown

NameError: name 'StrA' is not defined

After we removed the StrA, it now says that StrA is not defined.

That’s it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.