Tuesday, 19 February 2019

String Operation in Python

In Python String is a sequence of characters.

It is an object that represents a sequence of characters. You can create a string in python as shown below
Python Code:
x = "XMarksThe Spot"

String are constant. They cannot be changed once memory is allocated for them. Every time you perform operations on a String it rather creates a new string. Below example show you some common String operations in Python.
Python Code:
s = "XMarksTheSpot"
upper(s) # Prints XMARKSTHESPOT
lower(s) # Prints xmarksthespot
s.startswith("XMa") # Prints True
s.replace("X", "Y") # Prints YMarksTheSpot
len(s) # Prints 13
s[0] # Prints X
s[1] # Prints MarksTheSpot

We can slice parts of a string using slice operator :. The digit in front of the the : represents the start index. The digit after the : represents the end index. For example :
Python Code:
>>> s[1:3]
'Ma'

We can get list of characters for a given string using list function.
Python Code:
>>> list(s)
['X', 'M', 'a', 'r', 'k', 's', 'T', 'h', 'e', 'S', 'p', 'o', 't']

We can remove leading and trailing space from a string using strip function.
Python Code:
>>> x = " I have a lot of space before and after me...... do I?"
>>> x.strip( )
'I have a lot of space before and after me... do I?'

We can remove spaces from the left of a string using Istrip function.
Python Code:
>>>x.Istrip( )
' I have a lot of space before and after me ... do I? '

We can remove spaces from the right of a string using rstrip function.
Python Code:
>>> x.rstrip( )
' I have a lot of space before and after me ... do I? '

Join function on a string can join a list of strings using a specified delimiter. In the example below, we are joining a list of strings with comma as delimiter.
Python Code:
>>> ",".join (["Apple","a","day"])
'Apple,a,day'

We can check whether a given string starts with a substring using startswith method.
Python Code:
>>> y = "Superwoman"
>>> y.startswith("Duper")
False
>>> y.startswith("Super")
True

We can check whether a given string ends with a substring using endswith method.
Python Code:
>>> y.endswith("girl")
False
>>> y.endswith("woman")
True

We can use split method on a string to split a string into a list of substrings. To the split method, we need to pass a delimiter on which the split operation is to be performed.
Python Code:
>>> z="www.ntirawen.com"
>>>z.split(".")
['www','ntirawen','com']

zfill function is used to fill up the leading part of string with zeros until the specified with.
Python Code:
>> z.zfill(25)
'00000000www.ntirawen.com'

We can format a string using % operator. % is a format specifier used to specify how the data is to be formatted. In the below example, we are using %d format specifier which is an integer. 
Python Code:
>>> "There are %d letters in the alphabet" % 26
'There are 26 letters in the alphabet'

In the below example, we are using %s format specifier, which is a string, in addition to %d format specifier.
Python Code:
>>> "There are %d planets. The largest planet is %s" %(8,"Jupiter")
'There are 8 planets. The largest planet is Jupiter'

2 comments:

Popular Posts

Categories

Android (21) AngularJS (1) Books (3) C (75) C++ (81) Data Strucures (4) Engineering (13) FPL (17) HTML&CSS (38) IS (25) Java (85) PHP (20) Python (85) R (69) Selenium Webdriver (2) Software (13) SQL (27)