10X Sale
kh logo
All Courses
  1. Tutorials
  2. Programming Tutorials

Python - Strings

Updated on Sep 3, 2025
 
39,071 Views

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:

Image

'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 sequence

Description

Example

Result

\a

Bell or alert

"\a"

Bell sound

\b

Backspace

"ab\bc"

ac

\f

Formfeed

"hello\fworld"

hello world

\n

Newline

"hello\nworld"

hello world

\nnn

Octal notation, where n is in the range 0-7

'\101'

A

\t

Tab

'Hello\tPython'

HelloPython

\xnn

Hexadecimal 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:

Symbol

Purpose

%c

character

%s

string

%i

signed decimal integer

%d

signed decimal integer

%u

unsigned decimal integer

%o

octal integer

%x / %X

hexadecimal integer

%e / %E

exponential notation

%f

floating 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       '
+91

By Signing up, you agree to ourTerms & Conditionsand ourPrivacy and Policy

Get your free handbook for CSM!!
Recommended Courses