String Manipulations in Python | Part - 2

05 min read

Lowercase letters: string.lower() function

This converts all the characters in a string to lowercase alphabets.
Consider the example,

greet = 'Hello FACE Prep'
print(greet.lower())

Output:
hello face prep


Uppercase letters: string.upper() function

This converts all the characters in a string to lowercase alphabets.
Consider the example,

greet = 'Hello FACE Prep'
print(greet.upper())

Output:
HELLO FACE PREP
Replace:


In certain situations it is important that a particular variable is to be replaced by an another variable in the entire document. This couldbe done as follows in Python:

str1= "face prep"
str2 = str1.replace("e", "placements")
print(str2)

Output:
facplacemenets prplacementsp
Find:


find() helps us to find the index of the parameter passed inside find() function in the given string. The following example would help us understand the concept better.

str1 ="FACE Prep"
index1=str1.find("r")
print(index1)

Output: 6

Slice:

We can access the individual elements in the strings through their indices.
Note: In Python if the index is mentioned as [n:m] it calls the elements from n to m-1. (m is not counted)
Index always starts from zero.
Let us consider the following example,

str1 ="FACE Prep"
print(str1[:4]) # [:4] represents that from the start of the string till the index 3
print(str1[3:7]) # index elements from 3 to 6 are accessed.

Output:
FACE
E Pr
POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error