13-Strings - Understanding String in Build Methods and Operations (Slicing) - 12-04-2023
13-Strings - Understanding String in Build Methods and Operations (Slicing) - 12-04-2023
13-Strings - Understanding String in Build Methods and Operations (Slicing) - 12-04-2023
Python Programming
Dr. Arun Pandian J
Module 4: Python Strings & Regular
Expressions
SAME:
You can index a list or string by providing an integer
index value, beginning with 0 from the left or -1 from
the right [i].
You can slice a list or string by providing begin and
end values ([i:j]) or begin, end and step values ([i:j:k])
You can omit begin or end ([:j] or [i:]) to mean 'as far
as you can go'
List index vs string index (continued)
DIFFERENT:
if you reference a single element of a list with the index
operator ([i]), its type is the type of that element
>>> abc = ['a', 'b', 'c']
>>> abc[0]
'a'
>>> type(abc[0])
<class 'str'>
If you slice (extract a piece of) a list with begin and end
([i:j]) values, you get a sublist (type list)
>>> abc[0:2]
['a', 'b']
>>> type(abc[0:2])
<class 'list'>
String methods
A method is a function that is bundled together with a
particular type of object
A string method is a function that works on a string
This is the syntax of a method:
anObject.methodName(parameterList)
For example,
>>> 'avocado'.index('a')
0
returns the index of the first 'a' in 'avocado'
You can also use a variable of type string
>>> fruit = 'avocado'
>>> fruit.index('a')
0
Method parameters
Like any function, a method has zero or more parameters
Even if the parameter list is empty, the method still works
on the 'calling' object:
>>> 's'.isupper()
False
Here is a string method that takes two parameters:
>>> aStr = 'my cat is catatonic'
>>> aStr.replace('cat', 'dog')
'my dog is dogatonic'
Strings are immutable
A string is immutable -- once created it can not be
modified
When a string method returns a string, it is a different
object; the original string is not changed
>>> aStr = 'my cat is catatonic'
>>> newStr = aStr.replace('cat', 'dog')
>>> newStr
'my dog is dogatonic'
>>> aStr
'my cat is catatonic'
However, you can associate the old string name with the
new object
>>> aStr = 'my cat is catatonic'
>>> aStr = aStr.replace('cat', 'dog')
>>> aStr
'my dog is dogatonic'
String Methods
Input = “cab”
Output = Duplicate String
Input = “ccab”
Output = Unique string
Input = “ab”
Output = Unique string
Input:
Python Programming can be used to process text data for the requirements in various textual
data analysis. A very important area of application of such text processing ability of python is
for NLP. NLP is used in search engines, newspaper feed analysis and more recently for voice -
based applications like Siri and Alexa. Python's NLTK is a group of libraries that can be used
for creating such Text Processing systems.
Output:
Python Programming can be used to process text data for the requirements in various textual
data analysis.
A very important area of application of such text processing ability of python is for NLP.
NLP is used in search engines, newspaper feed analysis and more recently for voice -based
applications like Siri and Alexa.
Python's NLTK is a group of libraries that can be used for creating such Text Processing
systems.