Important String Methods

1 minute read

A string is a sequence of characters. In Python, a string is a sequence of Unicode characters. Unicode was introduced to include every character in all languages and bring uniformity in encoding. Strings can be created by enclosing characters inside a single quote or double-quotes. Triple quotes are used to represent multiline strings and docstrings.

accessing characters from a string

Just like lists and tuples we can access characters of a string by indexing and range of characters by slicing.

name = "Abhijit Tripathy"

# accessing single character
print(name[6])
# accessing range of characters
print(name[2:6])

# trying to access something out of the index
try:
    print(name[25])
except:
    print("An error occured")
OUTPUT :

t
hiji
An error occured
changing or deleting string

Like tuples and unlike lists strings are immutable. This means that elements of a string cannot be changed once they have been assigned. Deleting a string can be carried out by using the del keyword similar to that of the tuple.

Methods in string
Method Name Work of the method
endswith() Check if the string ends with some special character
capitalize() Convert the first character to capital letter
find() Returns the index of the first occurrence of the substring
index() Returns the index of the substring
join() Used for string concatenation
upper() Returns upper case string
lower() Returns lower case string

We are here to help ! Be sure to check our website and don’t hesitate to ask any questions on our community platform. We provide personal mentoring and teaching too, in order to upgrade your skills. Vist www.edualgoacademy.com to get started.

Spotted a bug ? Great job, you found a bug. Please report it to us in our mail and we’ll fix it as soon as possible.