19 Apr 2022: Unit 3: Strings PYTHON PROGRAMMING B.Tech IV Sem CSE A
Today’s Topics str class, string inbuilt functions, index[], traversing string: for, while, immutable strings, string operators, string operations
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'.
str examples name=str("Sreedhar") age=str(24) print(name) print(age) branch="computerscienceandengineering" print(branch) print(len(branch)) print(min(branch)) print(max(branch)) print(branch.center(40,'#')) print(branch.find('science')) Sreedhar 24 computerscienceandengineering 29 a u #####computerscienceandengineering###### 8
print(branch.find('science',8,18)) print(branch.find('m',0,10)) print(branch.rfind('engine')) print(branch.swapcase()) print(branch.startswith('science')) print(branch.startswith('computer')) print(branch) print(branch.capitalize()) print(branch.upper()) 8 2 18 COMPUTERSCIENCEANDENGINEERING False True computerscienceandengineering Computerscienceandengineering COMPUTERSCIENCEANDENGINEERING
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
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
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.
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
traversing string: for,while, branch="Computer Science" for char in branch: print(char,end="") string="Python Programming CSE4A" j = 0 while j < len(string): j += 1 print(string[0:j])
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.
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 !!
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)
G Pulla Reddy Engineering CollegeComputer Science and Engineering Computer Science and EngineeringComputer Science and Engineering True False True False
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
branch1="Computer Science and Engineering" branch2="Computer Science Technology" print(branch1>branch2) branch3="Computer Science Technology" print(branch2==branch3) print(branch2.split()) print(branch2.isalnum()) print(branch2.isalpha()) print(branch2.isdigit()) print(branch2.islower()) print(branch2.isupper()) print(branch2.isspace()) a="123test" print(a.isalnum()) print(a.isalpha()) b="45678" print(b.isdigit()) True True ['Computer', 'Science', 'Technology'] False False False False False False True False True
course="G Pulla Reddy Engineering College" print(course.endswith("College")) print(course.startswith("Programming")) print(course.find("Engineering")) print(course.count("e")) a="G Pulla Reddy Engineering Collegenntt" print(a.rstrip()) True False 14 5 G Pulla Reddy Engineering College

Python Programming Strings