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'

0 Comments:

Post a Comment

Popular Posts

Categories

AI (32) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (146) C (77) C# (12) C++ (82) Course (67) Coursera (198) Cybersecurity (24) data management (11) Data Science (106) Data Strucures (8) Deep Learning (13) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (21) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (46) Meta (18) MICHIGAN (5) microsoft (4) Nvidia (1) Pandas (3) PHP (20) Projects (29) Python (888) Python Coding Challenge (285) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses