03 Strings in Python
03 Strings in Python
2
Strings
• 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
Inbuilt String
functions
• 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 A SCII values
6
OUTPUT
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
print(c)
8
Operators on
String
• The Concatenate strings with the “*”
operator can create multiple concatenated
copies.
• Example:
>>> print("Python"*10)
PythonPythonPythonPythonPythonPython
PythonPythonPythonPython
9
String Slicing
• 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
String Slice
example H e l l o
s=“Hello” 0 1 2 3 4
-5 -4 -3 -2 -1
11
Strings are
immutable
• Strings are immutable character sets.
• Once a string is generated, you cannot
change any character within the string.
12
String Comparision
• We can compare two strings using comparision
operators such as ==, !=, <,<=,>, >=
• Python compares strings based on
their corresponding ASCII values.
13
Example of string
comparision
str1="green"
str2="black"
print("Is both Equal:",
str1==str2) print("Is str1> str2:",
str1>str2) print("Is str1< str2:", OUTPUT:
rstrip() strip()
16
i) Converting string
functions
captitalize() Only First character capitalized
17
Program:
str=input("Enter any string:")
print("String Capitalized:", str.capitalize())
print("String lower case:", str.lower())
print("String upper case:", str.upper())
print("String title case:", str.title())
print("String swap case:", str.swapcase())
Output:
19
Program:
a=input("Enter any string:")
print("Center alignment:",
a.center(20)) print("Left alignment:",
a.ljust(20)) print("Right alignment:",
a.rjust(20))
Output:
20
iii) Removing
whitespace
Returns a string with
lstrip() characters
leading whitespace
characters removed
Returns a string with
trailing whitespace
rstrip() characters removed
21
Program
a=input("Enter any string:")
print("Left space trim:",a.lstrip())
print("Right space trim:",a.rstrip())
print("Left and right
trim:",a.strip()) Output:
22
iv) Testing
String/Character
isalnum()
Returns true if all characters in string are alphanumeric and there is
atleast one character
24
v) Searching for
substring
Endswith() Returns true if the strings ends with the substring
25
Program
a=input("Enter any string:")
print("Is string ends with thon:", a.endswith("thon"))
print("Is string starts with good:", a.startswith("good"))
print("Find:", a.find("ython"))
print("Count:", a.count(“o"))
Output: