top
April flash sale

Search

Python Tutorial

A string is a built-in sequence data type. A string object is an ordered collection of Unicode characters put between single, double or triple quotes.There are different ways of literal representation of string as follows:'Hello Python' Out[1]: 'Hello Python' In [2]: #using double quotes "Hello Python" Out[2]: 'Hello Python' In [3]: #using triple quotes ''' Hello World Welcome to Python ''' Out[3]: '\nHello World\nWelcome to Python\n'Note that even though double or triple quotes are used, Python uses single quotes for internal representation. Triple quotes are helpful in forming a string of more than one lines. Triple quotes may be of triple single quotes ('''…''') or triple double quotes ("""…"""). For each physical line the string includes a newline character \n. When the string object is displayed using print() statement, the effect newline characters is visible.In [4]: #using triple quotes aString=''' Hello World Welcome to Python ''' print (aString) Out[4]: Hello World Welcome to PythonThe newline character \n is one of the escape sequences identified by Python. Escape sequence invokes an alternative implementation character subsequence to \. In Python \ is used as escape character. Following table shows list of escape sequences.Escape sequenceDescriptionExampleResult\aBell or alert"\a"Bell sound\bBackspace"ab\bc"ac\fFormfeed "hello\fworld"hello world\nNewline"hello\nworld"hello world\nnnOctal notation, where n is in the range 0-7'\101'A\tTab'Hello\tPython'HelloPython\xnnHexadecimal notation, where n is in the range 0-9, a-f, or A-F'\x41'AString is also built using Python’s built-in str() function.In [5]: str(10) Out[5]: '10'All strings are objects of built-in str class. The str class has a number of methods to perform various operations on string. Let us discuss string methods and their uses.Following group of string methods deal with rearrangement of casing of alphabets in a string.capitalize() Changes first letter of string to capitallower() All uppercase letters in string are converted to lowercase.swapcase() Changes the case of letter from upper to lower and vice versa.title() First letter of all words changes to uppercase and the rest are lowercase.upper() All lowercase letters in string changed to uppercase.In [6]: word='hello' word.capitalize() Out[6]: 'Hello' In [7]: line='HELLO WORLD' line.lower() Out[7]: 'hello world' In [8]: line='Hello World' line.swapcase() Out[8]: 'hELLO wORLD' In [9]: line='python is beautiful' line.title() Out[9]: 'Python Is Beautiful' In [10]: line='hello world' line.upper() Out[10]: 'HELLO WORLD'Following group of methods are Boolean in nature. They return either True or False.endswith()Determines if string ends with given substring.startswith()Determines if string starts with given substring.isalnum()Returns true if all characters are alphanumeric and false otherwise.isalpha()Returns true if all characters are alphabetic and false otherwise.isdigit()Returns true if string contains only digits.islower()Returns true if string has all cased characters in lowercase and false otherwise.istitle()Returns true if each word in string starts with uppercase character and others in lowercase.isupper()Returns true if all characters in string are in uppercase and false otherwise.In [11]: line="How are you?" line.endswith("?") Out[11]: True In [12]: line.startswith("How") Out[12]: True In [13]: string="AMD64" string.isalnum() Out[13]: True In [14]: string.isalpha() Out[14]: False In [15]: string="102.556" # . is not a digit string.isdigit() Out[15]: False In [16]: line="How are you" line.islower() Out[16]: False In [17]: line.istitle() Out[17]: False In [18]: line.isupper() Out[18]: FalseFollowing set of methods deal with find/replace operations within a string.find()Determine if a substring occurs in given string. Returns index if found and -1 otherwiseindex()Same as find(), but raises an exception if substring not found.rfind()Same as find(), but search string in reverse direction.rindex()Same as index(), but search string in reverse direction.replace()Replaces all occurrences of a given substring and replace with another.In [19]: string="Simple is Better Than Complex" string.find("an") Out[19]: 19 In [20]: string.find("be") Out[20]: -1 In [21]: string.index("is") Out[21]: 7 In [22]: string.index('Is') --------------------------------------------------------------------------- ValueError                                Traceback (most recent call last) <ipython-input-22-2f723114bb0b> in <module>() ----> 1 string.index('Is') ValueError: substring not found In [23]: string="Simple is Better Than Complex" string.replace("is","was") Out[23]: 'Simple was Better Than Complex'Following methods are defined for the purpose of alignment of characters in string.center()Returns a string padded with given character so that original string is centered to a specified width.expandtabs()replaces tab escape sequence in string with multiple spaces. default tab size is 8.ljust()string is padded with spaces on left and left-justified to a specified width.lstrip()Removes leading whitespaces in string.rjust() string is padded with spaces on right and right-justified to a specified width.rstrip()Removes trailing whitespaces of string.split()Splits given string according to delimiter character or substring (default is space)  and returns list of substrings.strip() Performs both lstrip() and rstrip() on string.In [24]: string="Computer" string.center(30, "*") Out[24]: '***********Computer***********' In [25]: string.ljust(30,"*") Out[25]: 'Computer**********************' In [26]: string.rjust(30,"*") Out[26]: '**********************Computer' In [27]: string = "Hello\tWorld" string.expandtabs() Out[27]: 'Hello   World' In [28]: string='    computer    ' string.lstrip() Out[28]: 'computer    ' In [29]: string.rstrip() Out[29]: '    computer' In [30]: string='192.168.001.001' string.split('.') Out[30]: ['192', '168', '001', '001']String formattingPython uses C style format specification symbols (%d, %f, %s etc) to construct a string by substituting these symbols by Python objects.In the example below, the symbols %s and %d in the string are substituted by values of objects in tuple outside the string, prefixed by % symbolIn [31]: name="Kiran" marks=95 "Hi my name is %s and I have secured %d percent marks in my B.E exam" %(name, marks) Out[31]: 'Hi my name is Kiran and I have secured 95 percent marks in my B.E exam'The formatting symbols which can be used in Python string are listed in table below:SymbolPurpose%ccharacter%sstring%isigned decimal integer%dsigned decimal integer%uunsigned decimal integer%ooctal integer%x / %Xhexadecimal integer%e / %Eexponential notation%ffloating point real numberIn numeric formatting symbols, width before and/or after decimal point can be specified.In [32]: x=10 y=1001.21 "x=%5d y=%10.3f" %(x,y) Out[32]: 'x=   10 y= 1001.210'By default string is aligned to left. To make it right aligned prefix – symbol to width in %s symbol.In [33]: string='computer' "%30s"%(string,) Out[33]: '                      computer' In [34]: "%-30s"%(string,) Out[34]: 'computer  Python 3.x has a format() method which is more efficient and elegant as far as formatting with variable substitution is concerned.Instead of C like % formatting operator, {} symbols are used as place holders.In [35]: name="Kiran" marks=95 "Hi my name is {} and I have secured {} percent marks in my B.E exam".format(name, marks) Out[35]: 'Hi my name is Kiran and I have secured 95 percent marks in my B.E exam'Format specification characters are used with : instead of %. It means to insert a string in place holder use {:s} and for integer use {:d}. Width of numeric and string variables is specified as before.In [36]: x=10 y=1001.21 "x={:5d} y={:10.3f}".format(x,y) Out[36]: 'x=   10 y= 1001.210'Alignment of strings is formatted by <, > and ^ symbols in place holder. They make the substituted string left aligned, right aligned or center aligned respectively. Default is < for left alignment.In [37]: string='computer' "{:<30s}".format(string,) Out[37]: 'computer                      ' In [38]: "{:30s}".format(string,) #default is < - left alignment Out[38]: 'computer                      ' In [39]: "{:^30s}".format(string,) Out[39]: '           computer       '
logo

Python Tutorial

Python - Strings

A string is a built-in sequence data type. A string object is an ordered collection of Unicode characters put between single, double or triple quotes.

There are different ways of literal representation of string as follows:Python code

'Hello Python'
Out[1]:
'Hello Python'
In [2]:
#using double quotes
"Hello Python"
Out[2]:
'Hello Python'
In [3]:
#using triple quotes
'''
Hello World
Welcome to Python
'''
Out[3]:
'\nHello World\nWelcome to Python\n'

Note that even though double or triple quotes are used, Python uses single quotes for internal representation. Triple quotes are helpful in forming a string of more than one lines. Triple quotes may be of triple single quotes ('''…''') or triple double quotes ("""…"""). For each physical line the string includes a newline character \n. When the string object is displayed using print() statement, the effect newline characters is visible.

In [4]:
#using triple quotes
aString='''
Hello World
Welcome to Python
'''
print (aString)
Out[4]:
Hello World
Welcome to Python

The newline character \n is one of the escape sequences identified by Python. Escape sequence invokes an alternative implementation character subsequence to \. In Python \ is used as escape character. Following table shows list of escape sequences.

Escape sequenceDescriptionExampleResult
\aBell or alert"\a"Bell sound
\bBackspace"ab\bc"ac
\fFormfeed "hello\fworld"hello world
\nNewline"hello\nworld"hello world
\nnnOctal notation, where n is in the range 0-7'\101'A
\tTab'Hello\tPython'HelloPython
\xnnHexadecimal notation, where n is in the range 0-9, a-f, or A-F'\x41'A

String is also built using Python’s built-in str() function.

In [5]:
str(10)
Out[5]:
'10'

All strings are objects of built-in str class. The str class has a number of methods to perform various operations on string. Let us discuss string methods and their uses.

Following group of string methods deal with rearrangement of casing of alphabets in a string.

capitalize()
 Changes first letter of string to capital
lower()
 All uppercase letters in string are converted to lowercase.
swapcase()
 Changes the case of letter from upper to lower and vice versa.
title()
 First letter of all words changes to uppercase and the rest are lowercase.
upper()

 All lowercase letters in string changed to uppercase.
In [6]:
word='hello'
word.capitalize()
Out[6]:
'Hello'
In [7]:
line='HELLO WORLD'
line.lower()
Out[7]:
'hello world'
In [8]:
line='Hello World'
line.swapcase()
Out[8]:
'hELLO wORLD'
In [9]:
line='python is beautiful'
line.title()
Out[9]:
'Python Is Beautiful'
In [10]:
line='hello world'
line.upper()
Out[10]:
'HELLO WORLD'

Following group of methods are Boolean in nature. They return either True or False.

endswith()
Determines if string ends with given substring.
startswith()
Determines if string starts with given substring.
isalnum()
Returns true if all characters are alphanumeric and false otherwise.
isalpha()
Returns true if all characters are alphabetic and false otherwise.
isdigit()
Returns true if string contains only digits.
islower()
Returns true if string has all cased characters in lowercase and false otherwise.
istitle()
Returns true if each word in string starts with uppercase character and others in lowercase.
isupper()
Returns true if all characters in string are in uppercase and false otherwise.
In [11]:
line="How are you?"
line.endswith("?")
Out[11]:
True
In [12]:
line.startswith("How")
Out[12]:
True
In [13]:
string="AMD64"
string.isalnum()
Out[13]:
True
In [14]:
string.isalpha()
Out[14]:
False
In [15]:
string="102.556" # . is not a digit
string.isdigit()
Out[15]:
False
In [16]:
line="How are you"
line.islower()
Out[16]:
False
In [17]:
line.istitle()
Out[17]:
False
In [18]:
line.isupper()
Out[18]:
False

Following set of methods deal with find/replace operations within a string.

find()
Determine if a substring occurs in given string. Returns index if found and -1 otherwise
index()
Same as find(), but raises an exception if substring not found.
rfind()
Same as find(), but search string in reverse direction.
rindex()
Same as index(), but search string in reverse direction.
replace()
Replaces all occurrences of a given substring and replace with another.
In [19]:
string="Simple is Better Than Complex"
string.find("an")
Out[19]:
19
In [20]:
string.find("be")
Out[20]:
-1
In [21]:
string.index("is")
Out[21]:
7
In [22]:
string.index('Is')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-22-2f723114bb0b> in <module>()
----> 1 string.index('Is')

ValueError: substring not found
In [23]:
string="Simple is Better Than Complex"
string.replace("is","was")
Out[23]:
'Simple was Better Than Complex'

Following methods are defined for the purpose of alignment of characters in string.

center()Returns a string padded with given character so that original string is centered to a specified width.
expandtabs()replaces tab escape sequence in string with multiple spaces. default tab size is 8.
ljust()string is padded with spaces on left and left-justified to a specified width.
lstrip()Removes leading whitespaces in string.
rjust() 
string is padded with spaces on right and right-justified to a specified width.
rstrip()Removes trailing whitespaces of string.
split()Splits given string according to delimiter character or substring (default is space)  and returns list of substrings.
strip() Performs both lstrip() and rstrip() on string.
In [24]:
string="Computer"
string.center(30, "*")
Out[24]:
'***********Computer***********'
In [25]:
string.ljust(30,"*")
Out[25]:
'Computer**********************'
In [26]:
string.rjust(30,"*")
Out[26]:
'**********************Computer'
In [27]:
string = "Hello\tWorld"
string.expandtabs()
Out[27]:
'Hello   World'
In [28]:
string='    computer    '
string.lstrip()
Out[28]:
'computer    '
In [29]:
string.rstrip()
Out[29]:
'    computer'
In [30]:
string='192.168.001.001'
string.split('.')
Out[30]:
['192', '168', '001', '001']

String formatting

Python uses C style format specification symbols (%d, %f, %s etc) to construct a string by substituting these symbols by Python objects.

In the example below, the symbols %s and %d in the string are substituted by values of objects in tuple outside the string, prefixed by % symbol

In [31]:
name="Kiran"
marks=95
"Hi my name is %s and I have secured %d percent marks in my B.E exam" %(name, marks)
Out[31]:
'Hi my name is Kiran and I have secured 95 percent marks in my B.E exam'

The formatting symbols which can be used in Python string are listed in table below:

SymbolPurpose
%ccharacter
%sstring
%isigned decimal integer
%dsigned decimal integer
%uunsigned decimal integer
%ooctal integer
%x / %Xhexadecimal integer
%e / %Eexponential notation
%ffloating point real number

In numeric formatting symbols, width before and/or after decimal point can be specified.

In [32]:
x=10
y=1001.21
"x=%5d y=%10.3f" %(x,y)
Out[32]:
'x=   10 y= 1001.210'

By default string is aligned to left. To make it right aligned prefix – symbol to width in %s symbol.

In [33]:
string='computer'
"%30s"%(string,)
Out[33]:
'                      computer'
In [34]:
"%-30s"%(string,)
Out[34]:
'computer  

Python 3.x has a format() method which is more efficient and elegant as far as formatting with variable substitution is concerned.

Instead of C like % formatting operator, {} symbols are used as place holders.

In [35]:
name="Kiran"
marks=95
"Hi my name is {} and I have secured {} percent marks in my B.E exam".format(name, marks)
Out[35]:
'Hi my name is Kiran and I have secured 95 percent marks in my B.E exam'

Format specification characters are used with : instead of %. It means to insert a string in place holder use {:s} and for integer use {:d}. Width of numeric and string variables is specified as before.

In [36]:
x=10
y=1001.21
"x={:5d} y={:10.3f}".format(x,y)
Out[36]:
'x=   10 y= 1001.210'

Alignment of strings is formatted by <, > and ^ symbols in place holder. They make the substituted string left aligned, right aligned or center aligned respectively. Default is < for left alignment.

In [37]:
string='computer'
"{:<30s}".format(string,)
Out[37]:
'computer                      '
In [38]:
"{:30s}".format(string,) #default is < - left alignment
Out[38]:
'computer                      '
In [39]:
"{:^30s}".format(string,)
Out[39]:
'           computer       '

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments

Eula

This is my first time here. I am truly impressed to read all this in one place.

Jaypee Dela Cruz

Thank you for your wonderful codes and website, you helped me a lot especially in this socket module. Thank you again!

lucky verma

Thank you for taking the time to share your knowledge about using python to find the path! Your insight and guidance is greatly appreciated.

Pre Engineered Metal Building

Usually I by no means touch upon blogs however your article is so convincing that I by no means prevent myself to mention it here.

Pre Engineered Metal Building

Usually, I never touch upon blogs; however, your article is so convincing that I could not prevent myself from mentioning how nice it is written.

Suggested Tutorials

Swift Tutorial

Introduction to Swift Tutorial
Swift Tutorial

Introduction to Swift Tutorial

Read More

R Programming Tutorial

R Programming

C# Tutorial

C# is an object-oriented programming developed by Microsoft that uses the .Net Framework. It utilizes the Common Language Interface (CLI) that describes the executable code as well as the runtime environment. C# can be used for various applications such as web applications, distributed applications, database applications, window applications etc.For greater understanding of this tutorial, a basic knowledge of object-oriented languages such as C++, Java etc. would be beneficial.
C# Tutorial

C# is an object-oriented programming developed by Microsoft that uses ...

Read More