String Operations in python

Ø  We can perform various operations on strings as below:

o   Slicing of String

o   Modify string

o   Concatenate Strings

o   Format strings

o   Escape Character

a) Slicing of String

Slice means a piece. [ ] operator is called slice operator, which can be used to retrieve parts of String. In Python Strings follows zero based index. The index can be either +ve or -ve. +ve index means forward direction from Left to Right. -ve index means backward direction from Right to Left

>>> s="Rais"

>>> s[0]

 

>>> s="Rais"

>>> s[1]

 

>>> # Slice From the Start

>>> b = "Online Smart Trainer!"

>>> print(b[:6])

 

>>> # Slice To the End

>>> a = "Online,Smart,Trainer"

>>> print(a[7:])

 

>>> s="Rais"

>>> s[-1]


b)   Modify String

Ø  Python has a set of built-in methods that you can use on strings.

Upper Case:

Ø   upper() method returns the string in upper case:

>>> a = "Online, Smart!"
>>> print(a.upper())

Lower Case:

Ø  The lower() method returns the string in lower case:

>>> a = "LORDS INSTITUTE OF ENGINEERING AND TECHNOLOGY!"
>>> print(a.lower())


Remove Whitespace

Ø  Whitespace is the space before and/or after the actual text, and very often you want to remove this space. The strip() method removes any whitespace from the beginning or the end:

# Remove Whitespace

>>> a = "   Online Smart Trainer!   "

>>> print(a.strip())

 

Replace String

Ø  The replace() method replaces a string with another string:

# Replace string

a = "Offline Smart Trainer!"

print(a.replace("Off", "On"))


Split String

Ø  The split() method returns a list where the text between the specified separator becomes the list items. The split() method splits the string into substrings if it finds instances of the separator:

# Split String

>>> a = "Abdul, Rais"

>>> print(a.split(","))




b)   Concatenate String

Ø  To concatenate, or combine, two strings you can use the + operator. Merge variable a with variable b into variable c:

>>> a = "Online"

>>> b = "Smart"

>>> c = a + b

>>> print(c)





Ø  To add a space between them, add a " ":

>>> a = "Online"

>>> b = "Smart"

>>> c = a + " " + b

>>> print(c)


b)   Format String

Ø  As we learned in the Python Variables chapter, we cannot combine strings and numbers like this:

 >>> age = 36
 >>> txt = "My name is John, I am " + age
 >>> print(txt)


Ø  But we can combine strings and numbers by using the format() method!. The format() method takes the passed arguments, formats them, and places them in the string where the placeholders {} are. Use the format() method to insert numbers into strings:

>>> age = 36
>>> txt = "My name is John, and I am {}"
>>> print(txt.format(age))



Ø  The format() method takes unlimited number of arguments, and are placed into the respective placeholders:

>>> quantity = 3
>>> itemno = 567
>>> price = 49.95
>>> myorder = "I want {} pieces of item {} for {} dollars."
>>> print(myorder.format(quantity, itemno, price))

















Ø  You can use index numbers {0} to be sure the arguments are placed in the correct placeholders:

>>> quantity = 3
>>> itemno = 567
>>> price = 49.95
>>> myorder = "I want to pay {2} dollars for {0} pieces of item {1}."
>>> print(myorder.format(quantity, itemno, price))


b)   Escape Character

Ø  To insert characters that are illegal in a string, use an escape character.An escape character is a backslash \ followed by the character you want to insert.An example of an illegal character is a double quote inside a string that is surrounded by double quotes:

>>> txt = "We are the so-called "Vikings" from the north."


Ø  To fix this problem, use the escape character \":

>>> txt = "We are the so-called \"Vikings\" from the north."

>>> print(txt)






Comments

Search related post on google