Jump Start With Python - Part 4 (Strings)

Note: This article is in continuation to my previous articles:
A string, traditionally is a sequence of characters, either used as literal constant or as some kind of variable. Technically, a string is a data type used in programming, such as an integer and floating-point unit, but is used to represent text rather than numbers. It is comprised of a set of characters that can also contain spaces and numbers. In Python, the string is among the most popular data types.
 
Various string operations are discussed below,
 
Creating a String
 
In Python, string can be created enclosing single, double and triple quotes. The following are the examples for the same,
 
 
Figure 1: Example for creating string (Input) 
 
 
Figure 2: Example of creating a string (Output)
 
Accessing a String
 
Python does not support character type, so character type elements are treated as string with length one (1) or sub-string. We can access individual characters in Python string using indexing and slicing for a range of characters.
 
Let us take an example of string, x="C# CORNER".
 
c#corner 
 
Python allows indexing in both forward as well as backward direction with the range starting from 0 in case of forward direction and, from -1 in case of backward direction respectively. To access a string, address of an index must be in range as well as an integer else it will result in IndexError or TypeError.
 
 
Figure 3: Example for accessing string (Input) 
 
 
Figure 4: Example for accessing string (Output)
 
 
String Operations
 
Updating or Deleting a String
 
Strings are immutable, i.e elements of a string cannot be changed once they are assigned, the only way to change/update a string is reassigning new string to original string.
 
 
Figure 5: Example for updating string (Input & Output)
 
As already discussed above, strings are immutable, so we cannot remove any particular element of a string but the whole string can be deleted using 'del' keyword.
 
 
Figure 6: Example for deleting string (Input & Output)
 
Concatenation
 
It is defined as joining two or more than two strings to make one string. Python provides 4 different ways to perform concatenation of strings and they are explained below,
 
 
Figure 7: Example for concatenation of strings (Input & Output)
 
String Iteration
 
String Iteration can be done in python using loops (typically for and if). Example for string iteration is as below:
 
 
 
Figure 8: Example for String Iteration
 
Escape Characters
 
Escape Characters are defined as combination of a backslash (\) followed by a alphabet of digit which are used to change the normal interpretation pattern of compiler or interpreter. For example: \n is used to get a newline.
 
The following are the escape characters supported by Python: 
  • Newline (\n
  • ' (\')
  • " (\")
  • Tab (\t)
  • Vertical Tab (\v)
  • Backspace (\b)
  • Escape (\e)
  • ASCII Bell Character/ Alert (\a)
  • Control-X (\cx | \C-x
  • Formfeed (\f)
  • Meta-Control-X (\M-\C-x)
  • Octal Notation (\nnn)
  • Carriage Return (\r)
  • Character X (\x)
  • Hexadecimal Notation (/xnn)
  • 16 Bit Unicode (\uxxxx)
  • 32 Bit Unicode (\uxxxxxxxx)
Formatting Strings
 
Python supports formatting values in strings which can involve most basic usage to very complex expressions. Python uses same syntax used in printf() function in C. The following are some format symbol and their implementations:
  • Character (%c)
     
     
  • String (%s)
     
     
  • Signed Decimal Integers (%d and %i)
     
     
  • Unsigned Decimal Integers (%u)
     
     
  • Octal Integer (%o)
     
     
  • Hexadecimal Integer (Lowercase: %x & Uppercase: %X)
     
     
  • Exponential Notation (Lowercase: %e & Uppercase: %E)
     
     
  • Floating Point Number (%f
     
Flags in String Formatting
  • By Argument (*)
     
    The argument specifies precision or width. 
     
     
  • Display Sign for Integer (+
     
String Methods
 
Python provides various built-in methods to manipulate strings. String Methods in Python are explained as below:
  • Capitalize Method
     
    Syntax: string_name.capitalize() 
     
     
  • Isalpha Method
     
    Syntax: string_name.isalpha()
     
     
  • Count Method
     
    It consists of three parameters and they are as below:
     
    1. sub: Substring to be searched.
     
    2. start: Index position to start the search from. By default, it's 0.
     
    3. end: Index position to end search at. By default its last index in string.
     
    Syntax: string_name.count(parameter)
     
     
  • Center Method
     
    This method returns a string with the string passed and fills character passed to center the original string passed along with fill character. It consists of two parameters and they are as in the following,
     
    1. width: Total width of the string.
     
    2. fillchar: Character to be used to fill the string. 
     
    Syntax: string_name.center(parameter1, parameter2)
     
     
  • Encode Method
     
    This method is used to encode the string. It consists of two parameters and they are as below:
     
    1. encoding: Encoding format eg. ASCII, base64, utf-8, etc.
     
    2. errors: Error handling scheme to be used. By default it's strict than other schemes that can be used are ignore, xmlcharrefreplace, backslashreplace, replace, etc.
     
    Syntax: string_name.encode(encoding, errors).
     
     
     
  • Decode Method
     
    This method is used to decode the string. It consists of three parameters and they are as below: 
     
    1. encoding: Encoding format eg. ASCII, base64, utf-8, etc.
     
    2. errors: Error handling scheme to be used. By default its strict scheme, other schemes that can be used are ignore, xmlcharrefreplace, backslashreplace, replace, etc.
     
    Syntax: string_name.decode(encoding, errors)
     
     
  • Endswith Method
     
    This method is used to check if the string ends with characters specified in suffix. It consists of three parameters and they are as below:
     
    1. suffix: Tuple or string to account as the suffix.
     
    2. start: Index of string to start checking from.
     
    3. end: Index of string to end checking at.
     
    Syntax: string_name.endswith(suffix, start, end)
     
     
  • Expandtabs Method
     
    This method returns string supplied with added tab characters where "\t" is specified in the string. It consists of one parameter and it is as below:
     
    1. tabsize: It is the size of the tab to be added to the string. By default, tabsize is 8.
     
    Syntax: string_name.expandtabs(tabsize)
     
     
  • Find Method
     
    This method is used to find a string/sub-string in a given string. It returns the index of string/sub-string specified if it is found else it gives -1. It consists of three parameters and they are as below:
     
    1. str: It is specific string/substring to be searched in parent string.
     
    2. beg: It is starting index. By default, it is starting of the string i.e. 0.
     
    3. end: It is ending index. By default, it is the ending of the string.
     
    Syntax: string_name.find(sub, beg, end)
     
     
  • Index Method
     
    This method is similar to find the method, only difference is that if string/sub-string is not found then it will give exception error else it will give the index of string/sub-string specified.
     
    Syntax: string_name.index(sub, beg, end)
     
     
  • Isalnum Method
     
     
    This method is used to check if the string contains alphanumeric characters or not. If the string contains alphanumeric characters it will return true else it will return false.
     
    Syntax: string_name.isalnum()
     
     
  • Isdigit Method
     
    This method is used to check if there are digits in a string. It will return true if digits are present and false if they are not.
     
    Syntax: string_name.isdigit()
     
     
  • Islower Method
     
    This method checks if all the characters in a string are lower case-based or not, If they are lowercase then it will return true else it will return false.
     
    Syntax: string_name.islower()
     
     
  • Isspace Method
     
    This method checks if the string only has whitespace or not. If there is whitespace then it will return true else it will return false.
     
    Syntax: string_name.isspace()
     
     
  • Istitle Method
     
    This method checks if string is titlecased or not i.e. all case-based characters in string following non-case based letters are uppercase and all other case-based characters are lowercase. It will return true if the string is titlecased else it will return false.
     
    Syntax: string_name.istitle()
     
     
  • Isupper Method
     
    This method is used to check whether all the case-based characters of a string are upper case or not. If they are the upper case then it will return true else it will return false.
     
    Syntax: string_name.isupper()
     
     
  • Isnumeric Method
     
    This method is used to check if the string consists of only numeric characters. It is similar to isdigit method, the only difference is that this method can only be used with Unicode objects. It will return true if all characters are numeric else it will return false.
     
    Syntax: string_name.isnumeric()
     
     
  • Isdecimal Method
     
    This method is used to check if the string consists of only decimal characters in Unicode objects only. It will return true if all characters are decimals else it will return false.
     
    Syntax: string_name.isdecimal()
     
     
  • Join Method
     
    This method is used to perform concatenation in existing string. In easy terms, it takes a list of things to join with string. It consists of one parameter and it is explained below:
     
    1. seq: It is sequence string that is to be joined/concatenated with existing string.
     
    Syntax: string_name.join(seq)
     
     
  • Ljust Method
     
    This method is used for left justification in a string of length specified as a parameter. The original string is returned if width (length specified) is less than or equal to the length of the string. It consists of two parameters and they are explained as below:
     
    1. width: This is string length after padding
     
    2. fillchar: Character to be filled to justify the string with respect to width. By default its whitespace.
     
    Syntax: string_name.ljust(width, fillchar)
     
     
  • Lower Method
     
    This method is used to convert all case-based characters in a string to lowercase.
     
    Syntax: string_name.lower()
     
     
  • Lstrip Method
     
    This method is used to strip all characters at the beginning (left) of string. By default, it will strip whitespace. It consists of one parameter and it is explained as below:
     
    1. char: Character that needs to be stripped.
     
    Syntax: string_name.lstrip(char)
     
     
  • Partition Method
     
    This method is used to divide the string into multiple strings on the arrival of the character specified. If the character is not found in the string then it will return the original string along with two empty strings. It consists of one parameter and it is explained as below:
     
    1. char: Character from whose position partition is to be performed.
     
    Syntax: string_name.partition(char)
     
     
  • Replace Method
     
    This method is used to replace a character/sub-string in a string with new character/sub-string. It consists of three parameters and they are explained as below:
     
    1. old: Existing character/sub-string that needs to be replaced.
     
    2. new: New character/sub-string that needs to be placed instead of old character/sub-string.
     
    3. max: For restricting the maximum number of replacements to be done.
     
    Syntax: string_name.replace(old, new, max)
     
     
  • Rfind Method
     
    This method returns the last index of sub-string specified. It will return the last index of sub-string if it is found else it will return -1. It consists of three parameters and they are explained as below:
     
    1. sub: Sub-string that needs to be found.
     
    2. start: Starting index position in parent string. By default, it's 0.
     
    3. end: Ending index position in parent string. By default its length of the string.
     
    Syntax: string_name.rfind(sub, start, end)
     
     
  • Rindex Method
     
    This method returns last index of sub-string specified else it raises an exception error. It is similar to rfind method. It consists of three parameters and they are explained as below:
     
    1. sub: Sub-string that needs to be found.
     
    2. start: Starting index position in parent string. By default, it's 0.
     
    3. end: Ending index position in parent string. By default its length of the string.
     
    Syntax: string_name.rindex(sub, start, end)
     
     
  • Rjust Method
     
    This method is used for the right justification in a string of length specified as a parameter. The original string is returned if the width is less than or equal to the length of the string. It consists of two parameters and they are explained as below:
     
    1. width: This is string length after padding
     
    2. fillchar: Character to be filled to justify the string with respect to width. By default its whitespace.
     
    Syntax: string_name.rjust(width, fillchar)
     
     
  • Len Method
     
    This method returns the length of the string specified.
     
    Syntax: len(string_name)
     
     
  • Max Method
     
    This method returns the maximum alphabet character from the string specified. It consists of one parameter and it is explained as below:
     
    1. str: String to check
     
    Syntax: max(str)
     
     
  • Min Method
     
    This method returns the minimum alphabet character from string specified. It consists of one parameter and it is explained below:
     
    1. str: String to check
     
    Syntax: min(str)
     
     
  • Rstrip Method
     
    This method returns the string in which all characters have been stripped from the end of the string. It consists of one parameter and it is explained below:
     
    1. chars: Characters to be trimmed from string specified.
     
    Syntax: string_name.rstrip(chars)
     
     
  • Split Method
     
    This method returns the list of all substrings in the input string specified, using delimiter string used as separator. By default, if no delimiter is specified then whitespace is considered as the delimiter. It consists of two parameters and they are explained below:
     
    1. str: Delimiter string used in splitting.
     
    2. num: Number of lines to be made.
     
    Syntax: string_name.split(str, num)
     
     
  • Splitlines Method
     
    This method returns a list with all the lines in string separated using (\n). It consists of one parameter and is explained below:
     
    1. num: Any number used to split strings from parent string.
     
    Syntax: string_name.splitlines(num)
     
     
  • Startswith Method
     
    This method checks whether a string starts with parameter string supplied or not. It will return true if it starts with parameter string else it will return false. Startswith method can be restricted by passing starting and ending indexes. It consists of three parameters and they are explained below:
     
    1. pstr: Parameter string to check-in string supplied
     
    2. beg: Starting index of matching parameter. By default, it is 0.
     
    3. end: Ending index of matching parameter.
     
    Syntax: string_name.startswith(pstr, beg, end)
     
     
  • Strip Method
     
    This method returns a string in which all characters have been stripped from the starting and ending of inputs string specified. It consists of one parameter and it is explained below:
     
    1. char: Character to be removed from the starting and end of the input string. By default its whitespace.
     
    Syntax: string_name.strip(char)
     
     
  • Swapcase Method
     
    This method returns a string in which all case-based characters have been swapped.
     
    Syntax: string_name.swapcase()
     
     
  • Title Method
     
    This method returns a string in which all first characters have been capitalized.
     
    Syntax: string_name.title()
     
     
  • Upper Method
     
    This method returns a string in which all case-based characters have been uppercased.
     
    Syntax: string_name.upper()
     
     
  • Translate Method
     
    This method returns a string in which all characters have been translated using a table (defined using maketrans method). It consists of two parameters and they are explained below:
     
    1. table: Translation table, it is made using maketrans method.
     
    Note: maketrans are part of class bytes since Python 3.0 and later, earlier it was part of the string.
     
    2. deletechars: Characters to be removed from input string. By default it is empty.
     
    Syntax: string_name.translate(table(   ,deletechars))
     
     
  • Zfill Method
     
    This method pads the input string specified with zeros on the left according to the width parameter defined. It consists of one parameter and it is explained below:
     
    1. width: Final width of the output string
     
    Syntax: string_name.zfill(width)