4. str class
Strings are objects of the str class. We can create a string using
the constructor of str class.
The syntax of str() is:
str(object, encoding='utf-8', errors='strict')
object - The object whose string representation is to be
returned. If not provided, returns the empty string
encoding - Encoding of the given object. Defaults of UTF-8
when not provided.
errors - Response when decoding fails. Defaults to 'strict'.
7. branches = 'cse, cst, csbs, ece'
branches.split()
branches.split(",") ['cse', ' cst', ' csbs', ' ece']
text = 'Binary Search'
print(text.rjust(20),"is a classic recursive algorithm")
text = 'Quick Search'
print(text.ljust(10),"is a classic recursive algorithm")
Binary Search is a classic recursive algorithm
Quick Search is a classic recursive algorithm
8. BASIC INBUILT PYTHON FUNCTIONS FOR
STRING
len(a) #Return length i.e. number of characters in string a
min(a) #Return smallest character present in a string
max(a) #Return largest character present in a string
9. Index[ ] operator
The indexing operator (Python uses square brackets to
enclose the index) selects a single character from a string,
characters are accessed by their position or index value.
10. index[] OPERATOR
a="Welcome to CSE4A students"
b=['Mango','per kg','value','is',204.14]
c=('Apple','per kg','value','is',103.24)
d=a[2]
print(d)
lastchar=a[-1]
print(lastchar)
print(b[2])
print(b[3-6])
print(b[-2])
print(c[0])
print(c[-1])
print(c[2-4])
print(b[7-8])
l
s
value
value
is
Apple
103.24
is
204.14
12. immutable strings
Character sequences fall into two categories, i.e.
mutable and immutable.
Mutable means changeable and immutable means
unchangeable.
Strings are immutable sequences of characters.
13. String operators
branch="Computer Science and engineering"
print(branch[0])
print(branch[:3])
print(branch[2:6])
print(branch[4:])
print(branch[:])
print(branch[::-1])
print(branch[4::-1])
print(branch[0:16:2])
branch+=" !!"
print(branch)
C
Com
mput
uter Science and engineering
Computer Science and engineering
gnireenigne dna ecneicS retupmoC
upmoC
Cmue cec
Computer Science and engineering !!
14. college="G Pulla Reddy Engineering College"
branch="Computer Science and Engineering"
print(college+branch)
print(branch*2)
a="Engineering" in college
print(a)
b="JNTU" in college
print(b)
c="Maths" not in branch
print(c)
d="Science" not in branch
print(d)
15. G Pulla Reddy Engineering CollegeComputer Science and Engineering
Computer Science and EngineeringComputer Science and Engineering
True
False
True
False
16. string operations
String Comparison
String .format() Method()
split() Method
Testing String
Searching Substring in a String
Methods to Convert a String into Another String
Stripping Unwanted Characters from a String
Formatting String