1
Strings: string slices, immutability,
string functions and methods, string
module
2
• String is a sequence of characters.
• String may contain alphabets, numbers and
special characters.
• Usually strings are enclosed within a single
quotes and double quotes.
• Strings is immutable in nature.
• Example:
a="hello world"
b=“Python”
3
• Python mainly contains 3 inbuilt string functions.
• They are
– len()
– max()
– min()
• len()- Find out the length of characters in string
• min()- Smallest value in a string based on ASCII values
• max()- Largest value in a string based on ASCII values
4
What is ASCII values
5
Example for Inbuilt string functions
name=input("Enter Your name:")
print("Welcome",name)
print("Length of your name:",len(name))
print("Maximum value of chararacter in your name",
max(name))
print("Minimum value of character in your name",min(name))
6
Enter Your name:PRABHAKARAN
Welcome PRABHAKARAN
Length of your name: 11
Maximum value of chararacter in your name R
Minimum value of character in your name A
7
• The + operator used for string concatenation.
Example:
a=“Hai”
b=“how are you”
c=a+b
print(c)
8
• The Concatenate strings with the “*” operator
can create multiple concatenated copies.
• Example:
>>> print("Python"*10)
PythonPythonPythonPythonPythonPython
PythonPythonPythonPython
9
• Slicing operation is used to return/select/slice
the particular substring based on user
requirements.
• A segment of string is called slice.
• Syntax: string_variablename [ start:end]
10
H e l l o
s=“Hello” 0 1 2 3 4
-5 -4 -3 -2 -1
11
• Strings are immutable character sets.
• Once a string is generated, you cannot change
any character within the string.
12
• We can compare two strings using comparision
operators such as ==, !=, <,<=,>, >=
• Python compares strings based on their
corresponding ASCII values.
13
str1="green"
str2="black"
print("Is both Equal:", str1==str2)
print("Is str1> str2:", str1>str2)
print("Is str1< str2:", str1<str2)
OUTPUT:
Is both Equal: False
Is str1> str2: True
Is str1< str2: False
14
• String formatting operator % is unique to
strings.
• Example:
print("My name is %s and i secured %d
marks in python” % ("Arbaz",92))
• Output:
My name is Arbaz and i secured 92 marks in
python
15
len() min() max() isalnum() isalpha()
isdigit() islower() isuppe() isspace() isidentifier()
endswith() startswith() find() count() capitalize()
title() lower() upper() swapcase() replace()
center() ljust() rjust() center() isstrip()
rstrip() strip()
16
captitalize() Only First character capitalized
lower() All character converted to lowercase
upper() All character converted to uppercase
title() First character capitalized in each word
swapcase() Lower case letters are converted to
Uppercase and Uppercase letters are
converted to Lowercase
replace(old,new) Replaces old string with nre string
17
Program:
str=input("Enter any string:")
print("String Capitalized:", [Link]())
print("String lower case:", [Link]())
print("String upper case:", [Link]())
print("String title case:", [Link]())
print("String swap case:", [Link]())
print("String replace case:",[Link]("python","python programming"))
Output:
Enter any string: Welcome to python
String Capitalized: Welcome to python
String lower case: welcome to python
String upper case: WELCOME TO PYTHON
String title case: Welcome To Python
String swap case: wELCOME TO PYTHON
String replace case: Welcome to python programming
center(width) Returns a string centered in a field of given width
ljust(width) Returns a string left justified in a field of given width
rjust(width) Returns a string right justified in a field of given width
format(items) Formats a string
19
Program:
a=input("Enter any string:")
print("Center alignment:", [Link](20))
print("Left alignment:", [Link](20))
print("Right alignment:", [Link](20))
Output:
20
iii) Removing whitespace
characters
Returns a string with
lstrip() leading whitespace
characters removed
Returns a string with
trailing whitespace
rstrip()
characters removed
Returns a string with
leading and trailing
strip() whitespace characters
removed
21
Program
a=input("Enter any string:")
print("Left space trim:",[Link]())
print("Right space trim:",[Link]())
print("Left and right trim:",[Link]())
Output:
22
Returns true if all characters in string are alphanumeric and there is
isalnum()
atleast one character
Returns true if all characters in string are alphabetic
isalpha()
Returns true if string contains only number character
isdigit()
Returns true if all characters in string are lowercase letters
islower()
Returns true if all characters in string are uppercase letters
isupper()
isspace() Returns true if string contains only whitespace characters.
Problem Solving and Python Programming 23
Program
a=input("Enter any string:")
print("Alphanumeric:",[Link]())
print("Alphabetic:",[Link]())
print("Digits:",[Link]())
print("Lowecase:",[Link]())
Output:
print("Upper:",[Link]())
24
Endswith() Returns true if the strings ends with the substring
Returns true if the strings starts with the substring
Startswith()
Returns the lowest index or -1 if substring not found
Find()
Count() Returns the number of occurrences of substring
25
a=input("Enter any string:")
print("Is string ends with thon:", [Link]("thon"))
print("Is string starts with good:", [Link]("good"))
print("Find:", [Link]("ython"))
print("Count:", [Link](“o"))
Output:
Enter any string : welcome to python
Is string ends with thon: True
Is string starts with good: False
Find: 12
Count: 3
26
• String module contains a number of functions to
process standard Python strings
• Mostly used string modules:
[Link]()
[Link]()
[Link]()
[Link]()
[Link]()
[Link]()
[Link]()
27
[Link]() Converts first character to Capital Letter
[Link]() Returns the Lowest Index of Substring
[Link]() Returns index of substring
[Link]() Checks Alphanumeric character
[Link]() Checks if All Characters are Alphabets
[Link]() Checks Digit Characters
[Link]() Checks if all Alphabets in a String are Lowercase
[Link]() returns if all characters are uppercase characters
[Link]() Returns a Concatenated String
[Link]() returns lowercased string
[Link]() returns uppercased string
len() Returns Length of an Object
ord() returns Unicode code point for Unicode character
reversed() returns reversed iterator of a sequence
text="Monty Python Flying Circus"
print("Upper:", [Link](text))
print("Lower:", [Link](text))
print("Split:", [Link](text))
print("Join:", [Link]([Link](test),"+"))
print("Replace:", [Link](text,"Python", "Java"))
print("Find:", [Link](text,"Python"))
print("Count", [Link](text,"n"))
29
Upper: “MONTY PYTHON FLYING CIRCUS”
Lower: “monty python flying circus”
Split: [„Monty‟, „Python‟, „Flying‟, „Circus‟]
Join : Monty+Python+Flying+Circus
Replace: Monty Java Flying Circus
Find: 7
Count: 3
30