String in Python​

In python String variable may be declared as in single quote or double quote.

name="Sharp Tutorial"
print(name)
print(name[2])

   Output:
   Sharp Tutorial.[ First Line output 

  Second line output: a [As it prints the character at given index]

There are some important Functions that can manipulate the string accordingly.

1) len(): It gives the length of the given string.

name = "Sharp Tutorial!"
print(len(name))

   Output: 15

2) lower()/upper(): lower converts the string into Lower Case similary upper() converts to Upper case.

name = "Sharp Tutorial!"
print(name.upper()) print(name.lower())

Output: 

SHARP TUTORIAL

 sharp tutorial

3) We can also find the substring from a given string.

name = "Sharp, Tutorial!"
print(name[3:5])

    Output:
    rp [first index included but not the last index so it gives the 3 and 4 index elements]

4) strip(): This function is used to remove leading and trailing whitespaces.

name=" Sharp Tutorial "
print(name.strip())

   Output:
   “Sharp Tuorial” [only leading and trailing spaces will be removed and between space will not be  affected].

5) replace: This method replaces the given string with another string.

name = "Sharp, Tutorial!"
print(name.replace("u", "o"))

   Output:
   Sharp, Totorial! [u replaced by o].

Click Here

Video/ C Introduction

Watch video in full size
Wordpress Social Share Plugin powered by Ultimatelysocial