Unit - 3 Pythhon String and Operators
Unit - 3 Pythhon String and Operators
Each character is encoded in the ASCII or Unicode character. So we can say that
Python strings are also called the collection of Unicode characters.
multiline_string = '''This is a
multiline
string in Python.
It spans multiple lines.'''
You can also use double triple-quotes (""") for multiline strings:
Multiline strings preserve the line breaks and formatting within them. They are
often used for docstrings (documentation within functions, modules, or
classes), writing SQL queries, or any situation where you need to include
multiple lines of text as a single string.
-1-
Unit3 – Python Strings & Operators
str = "HELLO"
print(str[0])
print(str[1])
print(str[2])
print(str[3])
print(str[4])
# It returns the IndexError because
6th index doesn't exist
print(str[6])
5. String Length:
• To get the length of a string, we can use the len() function. len()
function returns the length of the string.
-2-
Unit3 – Python Strings & Operators
• Example:
a = "Hello, World!"
print(len(a))
6. Check String:
• To check if a certain phrase or character is present in a string, we can
use the keyword in.
txt = “Python is very interesting subject."
print(“very" in txt)
It will returns true
txt="python is very interesting"
if "very" in txt:
print("very is present in the string")
if “Very" not in txt:
print(“Very is not present in the string")
7. Slicing String:
• You can return a range of characters by using the slice syntax.
• Specify the start index and the end index, separated by a colon, to
return a part of the string.
• b = "Hello, World!"
print(b[2:5])
• Get the characters from position 2 to position 5 (5 not included)
• Note: The first character has index 0.
-3-
Unit3 – Python Strings & Operators
-4-
Unit3 – Python Strings & Operators
txt = “python"
x = txt.center(20, “#")
print(x)
Output:
#######python#######
2. count() :
• It returns the number of occurences of substring in the specified
range. It takes three parameters, first is a substring, second a start
index and third is last index of the range. Start and end both are
optional whereas substring is required.
• Syntax:count(sub[, start[, end]])
Example:
txt = "Hello world"
c= txt.count(‘l')
print(c)
output:3
txt = “Python is easy.I love Python.python is intresting"
-5-
Unit3 – Python Strings & Operators
c= txt.count(‘Python')
print(c)
output:2
3. join() :
This method is used to concat a string with iterable object. It returns a
new string which is the concatenation of the strings in iterable. It throws
an exception TypeError if iterable contains any non-string value.
Example:
myTuple = (“Python”, ”Programming”, “Language”)
x = "#".join(myTuple)
print(x)
Output:
Python#Programming#Language
-6-
Unit3 – Python Strings & Operators
The smallest possible character is the capital letter A, since all capital
letters come first in Python. Our largest character is the lowercase
letter z.
Example:
x=”python”
print(max(x),min(x))
Output:y,h
5. replace():
Return a copy of the string with all occurrences of
substring old replaced by new.
Syntax:
replace(old, new[, count])
If the optional argument count is given, only the first count occurrences
are replaced.
txt="python is easy .I love python.python is intresting "
y=txt.replace("python","java")
print(y)
Output:
java is easy .I love java.java is intresting
-7-
Unit3 – Python Strings & Operators
txt=“PYTHON”
txt1=txt.lower()
print(txt1)
Output:
python
7. split():
Python split() method splits the string into a comma separated list. It
separates string based on the separator delimiter. This method takes
two parameters and both are optional.
split(sep=None, maxsplit=-1)
• sep: A string parameter acts as a separator.
• maxsplit: Number of times split performs.
Example:
txt="python is easy. i love python. python is interesting"
y=txt.split()
print(y)
['python', 'is', 'easy.', 'i', 'love', 'python.', 'python', 'is', 'interesting']
-8-
Unit3 – Python Strings & Operators
-9-
Unit3 – Python Strings & Operators
➔ not in Operator:
This operator tests if a value does not exist in a sequence.
Example:
my_list = [1, 2, 3, 4]
print(5 not in my_list) # True, as 5 is not present in the list
-10-