__author__ = "Narendra Boyina"
# -----------------------------------------------------------------------------
# Copyright (c) 2025 BR Technologies PVT LTD
#-----------------------------------------------------------------------------===================================================================================
PART -1===================================================================================
"""Topics to be covered in today's class:
--> What is string ?
--> Elements/characters accessing
--> Single element accessing
--> range of elements accessing
--> Step elements accessing
--> Built-in String Method
"""
"""A string in Python programming is simply a sequence of characters.
Think of it as a word, sentence, or even a paragraph, all wrapped up in quotation marks.
strings are homogeneous immutable sequence of characters
x = "python"
strings can be represented using single/ double/ triple quotations
(But triple quotations we are using to comment the multi lines.
==> it (""" """) is also string but Interpreter treated as multi-commented lines,
will not be executed """
# comparison of quoted strings on same value
# print('python' == "python")
# print("python" == '''python''')
# print("python" == """python""")
"""
############## Elements/characters accessing ###################
Syntax: variable = instance/object[SI : EI : step] # SI (starting Index/address) EI (Ending Index/address)
--> Index always starts with Zero ends with -1
"""
"""@@@@@ Single element accessing @@@@@@
Syntax: variable = instance[index]
"""
name = "Boyina Narendra" # Object has been created for string class
# print(type(name))
# print(id(name))
# print(len(name)) # no of characters including whitespace
# output = name[0] # variable = instance[index]
# print(output)
# print(name[2])
# print(name[5])
# print(name[-1]) # negative index
# print(name[-2])
# print("name value is : ", name)
"""
@@@@@ range of elements accessing @@@@@@
Syntax: variable = instance[SI : EI]
"""
b = "Data science"
# print(len(b))
# print(b[0:5]) # we have to give end index always number+1
# print(b[1:5])
b = "Data Science"
# print(b[5:15]) #
# print(b[4:7])
# print(b)
# print(b[ :5]) # omitting starting address
# print(b[5: ]) # omitting ending address
# print(b[ : ]) # omitting both
# print(b)
# print(b[ :-2]) # ending with negative index
# print(b[5:-4]) # starting with +ve, ending with -ve index
# some 3 or 4 word combinations
# try pint out words by index access methods use start index and end index
""" @@@@@ Step elements accessing @@@@@@
variable = instance[SI : EI: step]"""
b = "Datascience"
# print(b[ : :2]) # last parameter is related to difference (start, stop, step)
# print(b[4: :2])
# print(b[ :6:3])
# print(b[ :7:2])
# print(b[2:9:2])
# print(b[ : :4])
technology = "artificial intelligence"
# print(technology)
# print(technology[ : :-1]) # minus ("-") means pointer starts from reverse of the string
# print(technology[ : :-2])
# print(technology[ : :-3])
# ip = "BMC IP : 10.216.113.151"
# req_ip= ip[9:]
# print(req_ip)
# ################ Built-in String Methods ###################
a = "Python Extensively used in Data science and Artificial Intelligence"
# print(a,"\n", type(a))
# print(a.upper()) # Converts all lowercase letters in string to uppercase.
# print(a.lower()) # Converts all uppercase letters in string to lowercase.
# print(a.swapcase()) #converts all uppercase characters to lowercase and vice versa
# print(a.title()) # converts the first letter of every word to uppercase(capital)
# print(a.capitalize()) # converts Upper case the first letter in this sentence
# exercize: some words are available in the given strings, use in operator
'''
center(__width,[__fillchar]) :
=========================
The method center() returns centered in a string of length width.
Padding is done using the specified fill char.Default filler is a space.
(Or)
Returns a space-padded string with the original string centered to a total of width columns'''
a = "artificial intelligence"
x = " Raahi "
# print(x.center(15,"*"))
# print(len(a)) # length
# print(a.center(73))
# print(a.center(73, '*'))
# print(a.center(73, ' '))
# print(a.center(90, '$'))
# print(len("*************************artificial intelligence*************************"))
#####################################################################################
'''
obj.ljust(__width,[__fillchar]):
=============
This method returns the string left justified in a string of length width.
Padding is done using the specified fillchar (default is a space).
The original string is returned if width is less than len(s)'''
a = "BR techno solutions"
# print(a.ljust(70))
# print(a.ljust(70, "_"))
# print(a.ljust(70, ' '))
# print(a.ljust(70, '*'))
#####################################################################################
"""obj.rjust(__width,[__fillchar]):
================
Returns a space-padded string with the original string right-justified to a total of width columns.
This method returns the string right justified in a string of length width.
Padding is done using the specified filled char (default is a space).
The original string is returned if width is less than len(s)"""
a = "WebFramework"
# print(a.rjust(70, '*'))
# print(a.rjust(70))
===================================================================================
PART -2===================================================================================__author__ = "Narendra Boyina"
# -----------------------------------------------------------------------------
# Copyright (c) 2025 BR Technologies PVT LTD
#-----------------------------------------------------------------------------
"""Topics to be covered in today's class:
--> endswith(), startswith(), count(), lstrip(), rstrip(), strip(), index, find, (+), split(), join(), partition()
--> Data validation Technics
--> isidentifier, isalpha, isdigit, isupper, istitle, replace, max(), min()
--> Practice programs
"""
str1 = "python extensively used in data science and artificial intelligence"
# print(str1)
# print(str1.endswith("intelligence"))
# print(str1.endswith("science"))
# print(str1.endswith("Intelligence")) # python is case-sensitive
# # #################################################################################################
# print(str1.startswith("python"))
# print(str1.startswith("Python")) # python is case-sensitive
# ##################################################################################
# # real world exmaple
# file_name = "strings.py"
# print(file_name)
# # please check is python file or not
# res = file_name.endswith(".py")
# print(res)
'''
count() --> string in-built method
Syntax: obj.count(sub-str, start= 0,end=len(string))
the method count() returns the number of occurrences of substring sub in the range [start, end].
Optional arguments start and end are interpreted as in slice notation '''
str2 = "python is used in data science, and in artificial intelligence and also in Web-frameworks"
# print(str2.count('in'))
# print(str2.count('also'))
# print(str2.count('in', 3, 45)) # 3 is starting index 15 is the ending index
# print(str2.count('ce')) # 0 is starting index is the ending index
# print(str2.count('ce', 3, 45)) # 3 is starting index 15 is the ending index
# print(len(str2))
####################################################################################################
"""
find: returns the index if sub-string available otherwise returns -1
Syntax: obj.find(sub-string, beg=0, end=len(string))
Return Value : Index if found ,and -1 otherwise'''
"""
str3 = "python is extensively used in data science and artificial intelligence"
# print(len(str3))
b = "data"
# print(str3.find(b))
c = "Data" # case sensitive so, it gives us -1 as output
# print(str3.find(c))
# print(str3.find(c.lower()))
# d = "is"
# print(str3.find("is", 5, 45)) # search starts from 15 & end at 45
# e = "artificial"
# print(str3.find(e))
# ##############################################################
# b = "art"
# print(str3.rfind(b))
# e = "int"
# print(str3.rfind(e))
#####################################################################################
"""
lstrip() --> Removes unwanted data from Left hand side (based on input provided by manual test engineer)
rstrip() --> Removes unwanted data from right hand side (based on input provided by manual test engineer)
strip() --> Removes unwanted data from both sides """
# str4 = "********sensors are working properly without having any issue"
# print("input string -->", str4)
# print("output string left-->", str4.lstrip("*"))
# str4 = " sensors are working properly without having any issue***"
# print(str4.lstrip(" "))
str4 = "********sensors are working properly without having any issue*************"
# print(str4.rstrip("*"))
# str_5 = "********sensors are working properly without having any issue*************"
# print(str_5)
# str6 = str_5.strip("*")
# print(str6)
# try different combinations patterns
#####################################################################################
# str4 = "iiiiiiisensors are working properly without having any issueiiiiiiii"
# print(str4.strip("i"))
#####################################################################################
# str5 = "python"
# print(str5.zfill(15))
# print(str5.zfill(150))
# filled_data = str5.zfill(1500)
# print(filled_data, "\n", len(filled_data))
"""
index:
======
Syntax:obj.index(sub-str, beg=0 end=len(string))
Return Value : Index (int) if found otherwise raises ValueError exception if str is not found'''
Note: Return the lowest index in S where substring sub is found,
such that sub is contained within S[start:end]. Optional
arguments start and end are interpreted as access notation.
"""
str6 = "python extensively in data science and artificial intelligence"
# print(str6, "\n input_string length is : ", len(str6))
# print(str6.index("science"))
# print(str6.find("science"))
c = "Science" # case sensitive so
# print(str6.find(c))
# print(str6.index(c)) # it will give an exception (ValueError: substring not found)
# print(str6.index("science"))
# print(str6.index("science", 10, 25))
# print(str6[14:51])
input_string = "python extensively used in data science and artificial data intelligence"
# print(input_string[input_string.index("data"):input_string.index(" a")])
#####################################################################################
"""The + operator can be used for string concatenation """
lastname = "Boyina"
firstname = "Narendra"
fullname = lastname + firstname
# print(fullname)
# print(lastname+"_"+firstname)
#####################################################################################
# strings are immutable, so the += operator re-binds the reference to a new object
# s = "new"
# print(s)
# s += "found" # s = s+ "found"
# print(s)
# s += "land" # s = s +"land"
# print(s) # ==>ans "newfoundland"
#####################################################################################
"""
use split() to divide a string in to multiple sub-strings and store in list data type,
without any argument, split() divides on white spaces
inbuilt function #string.split(:) --> will return list
"""
str6 = "python extensively used in data science and artificial data intelligence"
# print(str6)
# e = str6.split()
# print(e) # split method returns list
# d = "Boyina-narendra Ravi-Swetha"
# e = d.split("-")
# print(e) # Whenever - comes it will separate as a string
# print(type(e))
" What is the return data-type of split method ? ---> Ans : List "
# full_name = "Radhika, Veeramallu "
# name_split = full_name.split(",")
# print(name_split)
"""
(if we give any parameter like : * & it will split when ever * comes it will separate the string)
2nd parameter(optional) in the split()is number of string need to be done,
if possibility is there then only it will separate the many strings'''
"""
data = "Line1--abcdef--Line2--abc-Line4--abcd--sravya"
# print(data.split("--"))
# print(data.split('--', 1))
# print(data.split('--', 2)) # first 2 occurrences
# print("Boyina narendra Mahalakshmi Raju".split())
#####################################################################################
"""
call the join()method on the list of strings
join()-ing on an empty separator is an important & fast way of concatenating collection of strings
# """
# s = "".join(["boyina", "narendra", "surendra","Sathwika"])
# print(s) # ans: "boyinanarendrasurendra"
# s = " ".join(["boyina", "narendra", "surendra","Sathwika"])
# print(s) # ans: "boyina narendra surendra Sathwika"
# s = "_".join(["boyina", "narendra", "surendra","Sathwika"])
# print(s) # ans: "boyina_narendra_surendra_Sathwika"
# print(s, type(s)) # ans: "boyina narendra surendra"
#####################################################################################
"""
The partition() method divides a string into three, around a separator: prefix,separator,suffix
partition() method returns tuple
Syntax: # String.partition("substring") ==> will return tuple
"""
# x = "unforgetable"
# s = x.partition("forget")
# print(s) # ans ('un', 'forget', 'able')
#
# a = "London:america".partition(":")
# print(a)
""" Tuple unpacking is useful to de-structure the result """
abc = "London:america".partition(":")
# print(abc)
# departure = abc[0]
# separator = abc[1]
# arrival = abc[2]
# print(departure)
# print(separator)
# print(arrival)
# departure, separator, arrival = "London:america".partition(":")
# print(departure, separator, arrival)
""" ############ Data validation Technics ############
isidentifier : Return True if the string is a valid Python identifier, False otherwise.
"""
# data = "Narendra_2"
# print(data.isidentifier())
# data = "Narendra-2"
# print(data.isidentifier())
###################################################################################################
'''The method isalpha() checks whether the string consists of alphabetic characters only
This method returns true if all characters in the string are alphabetic and
there is at least one character, false otherwise'''
# data_1 = "Harshitha" # No space & digit in this string
# print(data_1.isalpha())
# #
# data = "my name is Boyina Narendra Qualification is M.tech"
# print(data.isalpha())
#####################################################################################################
'''This method returns true if all characters in the string are numeric or character, false otherwise'''
x = "2022" # No space in this string
# print(x.isalnum())
# y = "narendra"
# print(y.isalnum())
data = "my name is Boyina Narendra Qualification is M.tech"
# print(data.isalnum())
#####################################################################################################
'''The method isdigit() checks whether the string consists of digits only'''
data = "123456" # Only digit in this string
# print(data.isdigit())
# data = "ABC123456" # Only digit should be in this string
# print(data.isdigit())
# print(data.isalnum())
# print("1234567".isdigit()) # True #Useful when converting to int
##################################################################################################
"""
Return True if the string is a lowercase string, False otherwise.
"""
# data = "my name is Boyina Narendra Qualification is M.tech"
# print(data.islower())
data = "this is string example wow ............!...."
# print(data.islower()) # returns true if all characters in the string are lowercase, false otherwise
###################################################################################################
"""
isupper() Return True if the string is an uppercase string, False otherwise.
"""
# data = "my name is Boyina Narendra Qualification is M.tech"
# print(data.isupper())
# data = "NARENDRA & MAHALAKSHMI"
# print(data.isupper())
###################################################################################################
'''This method returns true if the string is a
title cased string(each word 1st letter should capitalise,false otherwise'''
# data = "Narenadra Boyina"
# print(data.istitle())
# data = "My Name is Boyina Narendra Qualification is M.tech"
# print(data.istitle())
###################################################################################################
"""
Return True if the string is a whitespace string, False otherwise.
"""
# data = "my name is Boyina Narendra Qualification is M.tech"
# print(data.isspace())
# data = " "
# print(data.isspace())
###################################################################################################
'''This method returns true if all characters in the string are decimal, false otherwise.'''
# data="this2009"
# print(data.isdecimal()) # False
# data = "23443434"
# data = u"this2009" # To define a Unicode string, we use u before string.
# print(data.isnumeric())
###################################################################################################
"""The method max() returns highest alphabet in the string like x/y/z (based on ASCII conversion)
Capital A ==> ASCII value is 65
Small a ==> ASCII value is 97
"""
# a = "Mahalakshmi"
# print(max(a))
# b = "ManasaSirisha"
# print(min(b))
##################################################################################################
"""
replace:
=======
syntax: str.replace("sub-str", "replace_sub_str", [occurrence])
Return a copy with all occurrences of substring old replaced by new.
count
Maximum number of occurrences to replace.
-1 (the default value) means replace all occurrences.
If the optional argument count is given, only the first count occurrences are replaced.
"""
text = "The quick brown fox jumps over the lazy dog. The quick brown fox is quick."
modified_text = text.replace("quick", "fast") # In all occurrences sub-string will be replaced
# print("Original text:", text)
# print("Modified text:", modified_text)
"Replace the first 2 occurrences of 'quick' with 'fast'"
# modified_text = text.replace("quick", "fast", 2) # only 2 times sub-string will be replaced
# print("Original text:", text)
# print("Modified text:", modified_text)
# print('xyyxyyxyxyxxy'.replace('xy', '12')) # 12y12y1212x12 replaced in all occurrences
# print('xyyxyyxyxyxxy'.replace('xy', '12', 2))
# print('xyyxyyxyxyxxy'.replace('xy', '12', 4))
# Note1: if we encode strings by default it will encode as byte string Ex: b"narendra"
# Note2: By deafult encoding is utf-8
#################### Practice #######################
# s = "".join(["boyina", "narendra", "surendra"])
# print(s) # ans: boyinanarendrasurendra
# s = " ".join(["boyina", "narendra","surendra"])
# print(s) # ans: boyina narendra surendra
# x = ['Boyina', 'narendra', 'kumar', 'Akshat']
# y = "-".join(x)
# print(y)
# print(type(y))
# a = "Boyina:narendra-kumar:Akshat"
# print(a.split(":"))
# x = ["boyina", "narendra","surendra"]
# s = "-".join(x)
# print(s) #ans: boyina-narendra-surendra
#
x = ["boyina", "narendra", "surendra"]
# s = "-->".join(x)
# print(s) #ans: boyina-->narendra-->surendra
colors = ";".join(["#45ff23", "#2321fa", "#1298a3"])
# print(colors) # ans #45ff23;#2321fa;#1298a3
# colors = "-".join(["red", "green", "Yellow"])
# print(colors) # ans #45ff23;#2321fa;#1298a3
""" undescore as a dummy name for the separator"""
# departure, x, arrival = "London_america".partition("_")
# print(departure) # London
# print(arrival) # america
"""
Lowercase Alphabet and ASCII Values:
a: 97
b: 98
c: 99
d: 100
e: 101
f: 102
g: 103
h: 104
i: 105
j: 106
k: 107
l: 108
m: 109
n: 110
o: 111
p: 112
q: 113
r: 114
s: 115
t: 116
u: 117
v: 118
w: 119
x: 120
y: 121
z: 122
Uppercase Alphabet and ASCII Values:
A: 65
B: 66
C: 67
D: 68
E: 69
F: 70
G: 71
H: 72
I: 73
J: 74
K: 75
L: 76
M: 77
N: 78
O: 79
P: 80
Q: 81
R: 82
S: 83
T: 84
U: 85
V: 86
W: 87
X: 88
Y: 89
Z: 90
"""Author: Boyina Narendra
Supporting Author: M. Meera Sindhu
Request: If you find this information useful, please provide your valuable comments
No comments:
Post a Comment