Comprehensive Python

Table Of Contents:

Python Datatypes: Strings

In previous tutorial, we learnt about numbers in python, and luck enough, we were introduced to the python's math module.
Having done the calculations and trigonometrics, what we have to dive into in our python series is the Python String.

I am pretty sure that, in our Getting Started lesson, we looked at the basics of strings in Python.
if you didn't undergo through it, then head back and revise it, for this is the continuation from where we left.

Python Strings


Strings in python would be described as the normal text we use in our day-to-day life, in messages, letters, proposals, speeches etc.
unlike our day-to-day texts, Strings are quoted in single or double quotation marks (' or "). Just as with other datatypes in python, you do not need to declare a variable datatype to store a string datatype.
The code below will be a good example of python strings:

>>> stringvar = 'I am string, quoted in single quotes'
>>> stringvar2= "I am another string, qouted in double quotes
>>> print(stringvar)
>>> print(stringvar2)

Without taking much of the delays, lets take a quick look on what we can do with python strings.

Working With Python Strings

Python strings are much flexible to work with; keep in mind though that python strings are immutable (i.e You can not simply change the letter at its position) though.
Here are the quick look on how we can manipulate python strings.
Taken we have Python string as below:

>>> introduction = "My name is Amin"

Now you can print the string above to the screen as:
>>>print(introduction) # My name is Amin
You can also do slicing of the string; To slice a string, we use a slicing operator ( [] ) as in below.
The index of the slice starts with zero. However, a slice of 0 is the first letter of a string.
Don't understand yeah? Action speaks louder than words:
>>> introduction = "My name is Amin."
>>> print(introduction[0]) # M
>>> print(introduction[1]) # y
>>> print(introduction) # My name is Amin

You can also do slicing of the substring by indicating the starting and ending index of the slice as [start:end].
The endth index is optional... If you put a start index followed by colon (:), then python reads from starting index to the end of string.
When slicing the substring, the index on the endth index is not included.
Below is an example good to leave you well informed:
>>> introduction = "My name is Amin"
>>> print(introduction[0:7]) # My name
>>> print(introduction[3:7]) # name
>>> print(introduction[0:]) # My name is Amin

You can however manipulate string using slice and addition/concatenating operator (+). look at below example
>>> introduction = "My name is Amin"
>>> print(introduction[0:11]+" John") My name is John
>>> print("Please tell your "+introduction[3:7]) Please tell me your name.


Besides all this pleasant stuff, we can also use [] to reverse our string as:
>>> print("I am tired!"[::-1]) # !derit ma I
We can also use different advanced operators to manipluate string, some of which are: *, and % signs.
The asteric sign repeats the string by the number multiplied, and % sign prints the formatted string to the screen.
Look at the examples below:
>>> gate = "-"
>>> print(gate) # -
>>> print(gate*6) # ------
>>> print(gate*3) # ---

Using a % sign, you may come up with the following string manipulations.
>>> intro= "My name is %s "
>>> print(intro % "Amin") My name is Amin
>>> print(intro % "Jane") My name is Jane
>>> print("%s is very good language." % "python") python is very good language.

Python String Methods

Even though we were able to manipulate the string in different ways in above sections,
Srting we have some advanced methods and functions to work with python strings; some of them are:
The len() function : This function takes a string and returns the length of the string.
>>> len("How many are this letters?") # 26

.capitalize()
The capitalize() Python string module is responsible for turning a string into first letter being the upper case letter.
This is especially useful when we want to follow the rules of the grammar.
The best example of this method would be:
print("my name is Amin.".capitalize()) # My name is amin.
.center(width, fillchar)
As the name narrates, the center() method of the string class adjust the string to the specified center.
It takes two parameters, that are, the width and the fillchar.
The width parameter specifies how many characters should accumulate the left and right of the specified string,
and fillchar, on the other hand, determines what charcters should fill the specified width.
The best example would be:
>>> text = 'center text'
>>> print(text.center(10,"0")) # "0000000000center text0000000000"
>>> print(text.center(5," ")) # "     center text     "
>>> print(text.center(6,'a') # "aaaaaacenter textaaaaaa"

.count(sub, start, end)
The mere pleasant thing about python string methods is that most are self-explaining.
As the name narrates, the function count() conts the number of occurences of a given substring in the string.
The last parameters, that are, start and end are optional.
The sub is the actual substring to check for, start is the starting index of searching,
and end is where you would want the counting to end.
If only the starting point is given and not ending index, then the count will start from the given index to the end of the scring.
if the starting and ending index are not given, the search will start from the index 0 through the end of the string (-1).
>>> text = "There are two a's in this text"
>>> print(text.count('a')) # 2
>>> print(text.count('a',8)) # 1
>>> print(text.count('a',8,20)) # 1

.decode(encoding, errors)
The decode() function converts a string from one encoding to another.
The encoding will however be the default encoding used in the string, where as the errors defaults to strict.
Some of the possible error handlings may include "slashreplace","replace","ignore" etc. b"This is just binary encoded".decode(errors='replace') # This is just binary encoded.

.encode(encoding,errors)
This is the oposite of .decode() function and converts the string from one encoding to another.
The encoding might be any encoding, such as "UTF-8", "UTF-16","ascii","iso2022-jp","hz","latin-1","johab","koi8-u" and other tiresome lists.
>>> print("Letter 'a' in UTF-16 is","a".encode("UTF-16","replace")) # Letter 'a' in UTF-16 is '\xff\xfea\x00'
.endswith(suffix, start, end)
Just like .count(), the .endswith() string function traverse the string between start and end parameters,
only that this checks whether a slice [start,end] ends with a given suffix and returns True or False.
If the start or end is not given, python starts from the beginning of text till the end.
>>>print("Find the end".endswith('end')) # True
.expandtabs(tabsize)
The .expandtabs() function is a special case where tabs are expanded by spaces as provided in tabsize parameter.
If the tabsize is not given, the default tabsize of 8 spaces will be assumed.
>>> text = "This is \t tab"
>>> print(text.expandtabs()) # This is          tab
>>> print(text.expandtabs(4)) # This is      tab
>>> print(text.expandtabs(12)) # This is              tab

.find(sub, start, end)
Just like .endswith(), the .find() function returns the lowest index where the first occurence of sub parameter is found.
We may also tell python where to start and end the search by providing the last two parameters of the function.
If the passed substring is not present in the string, then python returns -1.
>>> string = "This is test string"
>>> print(string.find("is")) # 2
>>> print(string.find("is",4,-1)) # 5
>>> print(string.find("lie")) # -1

.format()
This function is an method of string class, and used to format the text as in below example:
>>> name = "My name is {name} Matola."
>>> print(name.format(name='Amin')) # My name is Amin Matola.
>>> print("There are {0} mangoes in the bag.".format(20)) # There are 20 mangoes in the bag.

.index(sub, start, end)
The .index() function works exactly as .find() function and returns the lowest index where the first occurence of sub parameter is found.
We may also tell python where to start and end the search by providing the last two parameters of the function.
The only difference though, is that, if the passed substring is not present in the string, then python raises a traceback(ValueError).
>>> string = "This is test string"
>>> print(string.index("is")) # 2
>>> print(string.index("is",4,-1)) # 5
>>> print(string.index("lie")) # ValueError: substring not found

.isalnum()
This function is a kinda boolean test. It checks if a string containing atleast one character is alpha numeric.
If so, then the function returns True, otherwise it returns False
>>> "am1".isalnum() # True
>>> "am1 ".isalnum() # False -- Space
>>> "".isalnum() # False -- Null
>>> " ".isalnum() # False -- Space

.isalpha()
Just like the .isalnum(), except that this checks if all characters in a string are alphabetical characters (a to z, A to Z), and there is atleast one character is the string.
If the test is true, the function returns True, otherwise returns False.
>>>print("am".isalpha()) # True.
Try changing code for .isalnum() above, and see the difference.
.isdigit()
This function is a boolean test, just like above functions. It checks if a string containing atleast one character is/are digit(s).
If so, then the function returns True, otherwise it returns False
>>> "am1".isdigit() # False
>>> "1".isdigit() # True
>>> "167562".isdigit() # True
>>> " ".isdigit() # False

.islower()
This function also does boolean test. It checks if a string containing atleast one character is in lower case.
If so, then the function returns True, otherwise it returns False
>>> "me".islower() # True
>>> "Me".islower() # False -- Uppercase 'M'
>>> "".islower() # False -- Null
>>> " ".islower() # False -- Space

.isspace()
This boolean function for space test. It checks if a string containing atleast one character is/are space(s).
If so, then the function returns True, otherwise it returns False
>>> "am1".isspace() # False
>>> "am1 ".isspace() # False
>>> "".isspace() # False -- Null
>>> " ".isalnum() # True -- Space

.istitle()
This function checks if a string containing atleast one character is in title case.
If so, then the function returns True, otherwise it returns False
>>>"am".istitle() # False
>>>"Am".istitle() # True
>>>"".istitle() # False -- Null
>>>" ".istitle() # False -- Space

.isupper()
This function also does boolean test. It checks if a string containing atleast one character is in lower case.
If so, then the function returns True, otherwise it returns False
>>> "me".isupper() # False
>>> "Me".isupper() # True -- Uppercase 'M' at beginning
>>> "".isupper() # False -- Null
>>> " ".isupper() # False -- Space

.join(iterable)
Unlike all the other functions above, the .join() is a special case, and I am fond of using it.
It takes the iterable and returns string which is a concatenation of the items in the irable,
concatenated with the value in the initial string.
>>> "".join([1,2,3,4,5]) # 12345
>>> "-".join([5,"April",2019]) # 5-April-2019
>>> "/".join(['C:','Users','Coder','Desktop']) # C:/Users/Coder/Desktop
>>> " ".isupper(["I","am","going"]) # I am going

.ljust(width, fillchar)
Works like .center(), but with .ljust(), the string is aligned to the left,
of a string whose length is the width passed to this function.
The right of the string is filled with the character(s) passed to the fillchar parameter of this function.
If the fillchar is not given, the default is space. >>> "Tired".ljust(10,'-') #"Tired----------"
>>> "Tired".ljust(10) #"Tired          "
.lower()
The .lower() function converts a string into all lower cases (a-z). >>> "AMIN".lower() # amin
.lstrip([char])
This function removes the characters passed to this function, to the left of the string.
If the char arguments are not passed, then spaces are assumed.
>>> "    This is spaced".lstrip() # "This is spaced"
>>> "---Hyphens remove---".lstrip("-") # "Hypehns remove---" (only to the left)

.partition(sep)
The .partition() function takes a seperator, and returns a tupple of three elements,Head, Sep and tail.
It splits the string where the first occurence of the seperator(sep) is found and returns the substring before the separator,
the separator itself, and the string after the separator.
If the seperator is not in the string, then it returns a tuple of the string itself, and other two empty elements (str,"","").
>>> Date = "05-April-2019"
>>> print(Date) # "05-April-2019"
>>> Date.partition("-") # ('05','-','April-2019')
>>> Date.partition(",") # ('05-April-2019','','')

.partition(sep)
The .partition() function takes a seperator, and returns a tupple of three elements,Head, Sep and tail.
It splits the string where the first occurence of the seperator(sep) is found and returns the substring before the separator,
the separator itself, and the string after the separator.
If the seperator is not in the string, then it returns a tuple of the string itself, and other two empty elements (str,"","").
>>> Date = "05-April-2019"
>>> print(Date) # "05-April-2019"
>>> Date.partition("-") # ('05','-','April-2019')
>>> Date.partition(",") # ('05-April-2019','','')

.replace(old, new, count)
This function replaces all the old substrings of a string to the given new substring.
If the optional parameter count is given, then only the first count occurences are replaced.
>>> "people kill people".replace("people","animals") animals kill animals >>> "people kill people".replace("people","animals",1) animals kill people
.rfind(sub, start, end)
Just like .find(), the .rfind() function returns index of sub in the string except that the highest index which is the last occurence of sub parameter is returned.
We may also tell python where to start and end the search by providing the last two parameters of the function.
If the passed substring is not present in the string, then python returns -1.
>>> string = "This is test string"
>>> print(string.rfind("is")) # 5
>>> print(string.rfind("is",0,4)) # 2
>>> print(string.rfind("lie")) # -1

.rindex(sub, start, end)
The .rindex() function works exactly as .rfind() function and returns the highest index where the last occurence of sub parameter is found.
We may also tell python where to start and end the search by providing the last two parameters of the function.
The only difference though, is that, if the passed substring is not present in the string, then python raises a traceback(ValueError).
>>> string = "This is test string"
>>> print(string.rindex("is")) # 5
>>> print(string.rindex("is",0,4)) # 2
>>> print(string.rindex("lie")) # ValueError: substring not found

.rjust(width, fillchar)
Works like .ljust(), but with .rjust(), the string is aligned to the right,
of a string whose length is the width passed to this function.
The left of the string is filled with the character(s) passed to the fillchar parameter of this function.
If the fillchar is not given, the default is space. >>> "Tired".rjust(10,'-') #"----------Tired"
>>> "Tired".rjust(10) #"          Tired"
.rpartition(sep)
The .rpartition() function takes a seperator, and returns a tupple of three elements,Head, Sep and tail.
It splits the string where the last occurence of the seperator(sep) is found and returns the substring before the separator,
the separator itself, and the string after the separator.
If the seperator is not in the string, then it returns a tuple of the string itself, and other two empty elements (str,"","").
>>> Date = "05-April-2019"
>>> print(Date) # "05-April-2019"
>>> Date.rpartition("-") # ('05-April','-','2019')
>>> Date.rpartition(",") # ('05-April-2019','','')

.rsplit(sep,maxsplit)
The .rsplit() function returns a list of substrings (words), by deviding given string into smaller chuncks using a given seperator (sep).
if maxsplit is given, the split only occurs for maxsplit times to the right.
If a separator is not given, python splits the string using a space; and if number of count is not given, the split happens for the whole string.
>>> sentence = "I am going home"
>>> print(sentence.rsplit()) # ["I","am","going","home"]
>>> print(sentence.rsplit(" ",2)) ["I am","going","home"]

.rstrip([char])
This function removes given char(s) to the right of the string. If char is not given, then removes spaces to the right of the string.
If the given char is a unicode, then the whole string will be converted to unicode.
" Remove right space ".rstrip() # " Remove right space"

.split(sep,maxsplit)
Just like .rsplit(), this function returns the list of words by deviding a string into smaller chunks using a given sep .
If maxsplit is given, the split only happens for maxsplit times, else, the whole string will split.
If sep is not given, the sep will then default to whitespace.
>>> sentence = "I am going home"
>>> print(sentence.split()) # ["I","am","going","home"]
>>> print(sentence.split(" ",2)) ["I",am","going home"]

.splitlines(keepends=False)
Just like split(), this function returns the list of lines by deviding a string into lines using a newline('\n') as a separator.
The '\n' however is not included in the resulting list except if keepends keyword argument is turned True. >>> story = "I am going to school.\nAfter that, I'll go home."
>>> print(story.splitlines()) # ["I am going to school.","After that, I'll go home."]
>>> print(story.splitlines(True)) ["I am going to school.\n","After that, I'll go home."]

.startswith(prefix, start, end)
Does the opposite of .endswith() function; this function checks whether a string is starting with the given substring prefix
If start, and end parameters are passed, then the string to check from is only the slice of a string by start and end: string[start:end]. It turns True if a given string is really starting with the prefix, else, returns False.
If you have more than one prefix to check, then you can pass the possible prefixes as a tuple
>>> print("I am going".startswith("i")) # False -- lowercase
>>> print("I am going".startswith("am",2,6)) # True
>>> print("I am going".startswith(("Me","You","I")) # True

.strip([char])
Just like .rstrip(), this function is generic, and removes all white spaces to the Beginning and End of the string.
If char is given, then the char(s) are removed to the begining and end of the string.
>>> string = " spaces in here! "
>>> print(string.strip()) # 'spaces in here'
>>> print("---center---".strip('-')) # 'center'

.swapcase()
This function returns a copy of a string, and all the uppercase characters are converted to the lowercase,
and lowercase characters to uppercase.
>>> "i NOW KNOW python".swapcase() # 'I now know PYTHON'

.title()
This function returns a copy of a string in which all the first letters of each word is in uppercase.
>>> "I am gone".title() # 'I Am Gone'

.translate(table)
This function changes/translates a string charaters from into another characters.
This function works in auxilliary with .maketrans() function of string, which takes the first set of characters as the text which needs to be replaced, the second set of characters of which to replace with, and the characters to delete.
>>> question = "How are you?"
>>> table = question.maketrans("ear","swa")
>>> print(question.translate(table)) # "How was you?"

.upper()
The .upper() function does the opposite of .lower(),
and converts a string from lowercases to all upper cases (A-Z). >>> "amin".upper() # AMIN
.zfill(width)
Python string function which adds padding of a given string with zeros to fill up the given width of characters.
If String is less than or equal to the width, then no padding takes place because the string itsel is never truncated.
>>> "125".zfill(5) # "00125"

In python, strings are of high value, and have alot more functionalities than this, but what this chapter has covered,
is just enough for you to be called a string hacker because what is remaining is that we delve a string module.
Preventing much of spoon feeding, I leave you busy finding out about the string module before you tag along with lists.
Now the Booom!!! The course is on it's best, Lists in the stage.