Strings in Python
Definition of Strings
Strings in Python are a sequence of one or more characters. These characters can be alphabets, digits and
special characters. A string can be created by enclosing one or more characters in single, double or triple
quotes. We usually use triple quotes for strings that span multiple lines.
Example:
# Using single quotes
choice = 'Y'
# Using double quotes
school = "The Newtown School"
# Using triple quotes for multiline strings
country = """India is our country, it has a rich
cultural heritage. We must value it and make
efforts to preserve it"""
Accessing Characters in String:
Every character in a string is assigned a unique number which is called index or subscript. The index of the
first character (from left) in the string is 0.
Consider the following string:
S = "THE NEWTOWN"
T H E N E W T O W N
INDEX 0 1 2 3 4 5 6 7 8 9 10
If the length of the string is n, then the index of the last character in the string is always n-1, Here, the length
of the string is 11 and the index of the last character is 10. Note that space is also a character and has an
index associated with it.
S = "THE NEWTOWN"
T H E '' N E W T O W N
S[0] S[1] S[2] S[3] S[4] S[5] S[6] S[7] S[8] S[9] S[10]
For Example:
Write a program that accepts a string and then displays the string character by character.
Code
Output
FYI: Python supports negative indices also. The index of the last character in the string is -1, the index of
the second last character is -2 and so on.
S = "THE NEWTOWN"
T H E '' N E W T O W N
S[-11] S[-10] S[-9] S[-8] S[-7] S[-6] S[-5] S[-4] S[-3] S[-2] S[-1]
For Example:
Write a program that accepts a string and then displays the string using negative indices.
Code
Output
String Operations
Python provides many basic operations which can be performed on strings. Some of the operations are as
follows:
Concatenation
Concatenation operator is used to join two strings. The operator used to perform concatenation is denoted
by the '+' symbol. The adjacent figure shows the use of concatenation operator.
Example:
Repetition
Repetition operator is used to repeat a given string for a specified number of times. The operator used for
repetition is denoted by the '*' symbol.
Example:
Membership
Python has two membership operators: 'in' and 'not in'. These operators are used to check whether the
substring is present in the given string or not.
Operator Description Example
in Return True if the substring is
present in the given string and
False otherwise.
not in Returns True if the substring is
not present in the given string
and False otherwise.
Slicing
Slicing operator is used to extract a part of a string. In slicing, we specify the start and end index numbers
with the step value to specify the characters to be extracted.
Syntax: string[start : stop : step]
start: Starting index from where the slicing of string starts.
stop: Ending index where the slicing of string stops. The character at the stop index is not included
in the result.
step: It specifies the value by which the index is incremented. The default step value is 1.
[Note: The start, stop and step values are optional.]
Examples:
St = "Newtown School"
Expression Output Explanation
St[3 : 7] 'town' Start index: 3
Stop index: 7
Step value: 1
Note that the character at stop index, i.e., at index
7 is not included in the output.
St[ : 4] 'Newt' Start index: 0
Stop index: 4
Step index: 1
When the start index is not specified, the slicing
starts at index 0.
St[7 : ] ' School' Start index: 7
Stop index: till the end of the string
Step value: 1
When the stop index is not specified, the slicing is
done till the end of the string.
St[ : ] 'Newtown School' Start index: 0
Stop index: till the end of the string
Step value: 1
Slicing starts at the beginning of the string and is
done till the end of the string.
St[0 : 6 : 2] 'Nwo' Start index: 0
Stop index: 6
Step value: 2
Every second character is displayed starting from
index 0 till the index 6 [index 6 is not included].
St[ : : 3] 'Ntnco' Every third character is displayed starting from
index 0 till the end of the string.
BUILT-IN FUNCTION AND STRING METHODS
Python has a number of built-in functions and string methods that make it easier to work with strings.
Function/Method Description Example
len( ) Returns the number of characters in the
string.
title( ) Returns the string with first letter of
every word in the string in uppercase and
rest in lowercase.
lower( ) Returns the string with all alphabets in
lowercase.
upper( ) Returns the string with all alphabets in
uppercase.
count('substring') Returns number of times the substring
specified in the argument occurs in the
given string.
replace(old, new) Returns a new string in which all
occurrences of old string are replaced with
new string.
isupper( ) Returns True if the string is not an empty
string and has all uppercase alphabets.
islower( ) Returns True if the string is not an empty
string and has all lowercase alphabets.
TRAVERSING STRING
We can iterate or go through the various characters of a string by using a for or a while loop.
Code Output
PROGRAM USING STRINGS
Program1: To display # after every character of a string.
Code Output
Program2: To display the characters of a string in the reverse order.
Code Output
Program3: To count the number of times a character occurs in the string.
Code Output
Program4: To display the count of uppercase alphabets in the string.
Code Output
Program5: To display vowels in uppercase and consonants in lowercase.
Code Output