Innomatics Research Labs
Im a innomatics student, this is for innomatics research labs notes (the best institute of hyderabad) once search in google, course = data science and full stack development
Innomatics Research Labs is a pioneer in “Transforming Career and Lives” of individuals in the Digital Space by catering advanced training on IBM Certified Data Science, Python, IBM Certified Predictive Analytics Modeler, Machine Learning, Artificial Intelligence (AI), Full-stack web development, Amazon Web Services (AWS), DevOps, Microsoft Azure, Big data Analytics, Digital Marketing, and Career Launching program for students who are willing to showcase their skills in the competitive job market with valuable credentials, and also can complete courses with a certificate.
Strings:
- strings are a sequence or a char, enclosed in qoutes.
creating a string
a string can be created by
encloing a char or sequence of chars
insingle qoutes
ordouble qoutes
ortriple single qoutes
ortriple double qoutes
we use multi-line stings for documentation purpose; doc_strings
strings can be indexed.
since strings are a sequence of chars, and this seq has an order. We can access individgual chars by their index or position in that seq enclose in
[]
.x = 'python'
string
x
has a len of 6, (len of string is equals to the number of chars in that string) the first index is0
(indexing in python starts with0
)
len of sting x is 6
p
reverse indexing
- we can also access the chars in a string using reverse indexing.
- reverse indexing starts at the
end of the string
, reverse indexing starts with-1
- positive index starts with
0
and ends atlen(string)-1
- negative index starts with
-1
and ends at-len(string
- giving a number which doesn't fall in the given range will result
in IndexError
slicing
as the word suggest, we can get a sub-string of the given string by performing slicing
to slice a string, we need three values:
start_index
,end_index
,step_size
start_index
: from where the slicing has to start, or the starting index of sub-string.end_index
: in slicing, end index is end index - 1.end_index
tells till which index the slicing has to happen, (we are not going to include char at end_index)step_size
: it talks about how many steps it has to move while performing the slicing. by default, step_size is+1
syntax for slicing:
- var[start_index: end_index: step_size]
Pto
sredoc fo noitalupop egral a yb nekops ti ,nrael ot egaugnal taerg a si nohtyP
- while using a negative step size, we travel from end to start.
- the start and end index values also follow this.
- while using -ve step size, the default start and end indicies are (-1 or len(string)-1) and (-len(string) or 0) respectively
string concatenation
'hi everyone'
membership operations on strings
True
True
Methods in strings
- when we assign a string to a variable, we are basically performing an object initialization. By doing this, we can invoke or use the methods that are there in
str
class. - string methods are inbuilt prewritten functions that acts on the given string to get a desired output.
- results we get from string methods has to be saved in another variable to access them later as strings are immutable and these results have a different memory location than the given string.
<class 'str'>
upper()
upper()
is goint to convert lowercase alphabets in given string to uppercase.
ABC
abc
ABC
lower()
lower()
converts upper_case alphabets to lower_case
ABC
abc
capitalize()
- if an alphabet is at 0th index, it is going convert it to capital case
abc def
Abc def
*abc def
*abc def
a
b
c
A
b
c
title()
- title() going to convert every first alphabet of every consecutive word separeted with a space to capital case.
momma, i killed a man!
Momma, I Killed A Man!
a
b
c
A
B
C
swapcase()
- swapcase() is going swap the cases of alphabets
AbCd EfGh
aBcD eFgH
count()
- it will return the value of occurences of given sub-string in a string.
23
17
startswith() and endswith()
- these methods will return a boolean output.
- True, when the char or sub-string is passed is at the start and end of the string respectively
- False, otherwise
these
True True False
True True False
find and index
both methods will return the
index position
offirst occurence
of given char or sub-string.find and index performs same task, however they behave differently when a char or sub-string which is not present in the parent string is given as an input/arguement.
find
will return -1index
will return an error
22 180 -1
22 180
rfind and rindex
both methods will return the
index position
offirst occurence
of given char or sub-string from the right side of the string.rfind and rindex performs same task, however they behave differently when a char or sub-string which is not present in the parent string is given as an input/arguement.
rfind
will return -1rindex
will return an error
158 180 -1
158 180
replace()
- replaces every occurence of given char or sub-string with new char or sub-string
abc abc abc
Abc Abc Abc
aBC aBC aBC
abc abc abc
split()
- split() returns a list of sub-strings which were broken/splitted on given char or sub-string, if no arg/input is provide, it'll split on space.
['abc', 'def', 'ghi']
['abc d', 'f ghi']
partition()
- partition() splits on the given char or sub-string.
- it'll break the string on the given char or sub-string into three parts and returns it as a tuple.
('abc d', 'e', 'f ghi')
('abc', ' ', 'def ghi')
rpartition() abd rsplit()
('abc d', 'e', 'f ghi')
('abc def', ' ', 'ghi')
['abc d', 'f ghi']
['abc', 'def', 'ghi']
center
it is going to bring or readjust the given string to be at center of the width provided with spaces on either side (if no char input/arg is given) or with the given char
the width given as an input/arg should be greated than len(string), if not, the orginal string will be given as input
python
python
*******python*******
ljust() & rjust()
- the width given as an input/arg should be greated than len(string), if not, the orginal string will be given as input
python
python
python**************
python
**************python
zfill()
- zfill() is used to pad or fill the remaining of given width with zeros on left side
python
00000000000000python
strip()
it removes the whitespaces at the start and end of the string
if a char or a sub-string is given as input/arg, it'll removes the given char/sub-string
lstrip() and rstrip()
lstrip() is used to remove the prefix, it removes the given char/s if they are at start of the string. (space, if no char/s are provided)
rstrip() is used to remove the suffix, it removes the given char/s if they are at end of the string. (space, if no char/s are provided)
*******python*******
python*******
*******python
removeprefix() and removesuffix()
- removeprefix() and removesuffix() will remove only given char, but not the sequence of given char like in lstrip() and rstrip()
join()
- join() is going to join given sequence of chars using the string on which it is called.
python is great lang :/
['python', 'is', 'great', 'lang', ':/']
pythonisgreatlang:/
python**is**great**lang**:/
methods with boolean output
False
True
False
True
False
True
False
Comments
Post a Comment