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, string is among 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 for creating 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 example of string,
x="C# CORNER".
Python allows indexing in both forward as well as backward direction with 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 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 provide 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 back slash (\) 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 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 (*)
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 search from. By default its 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 string with the string passed and fill 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 its 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 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 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 size of tab to be added to 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 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/sub-string to be searched in parent string.
2. beg: It is starting index. By default it is starting of string i.e. 0.
3. end: It is ending index. By default it is ending of string.
Syntax: string_name.find(sub, beg, end)
![]()
- Index Method
This method is similar to find 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 string contains alphanumeric characters or not. If 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 lower cased then it will return true else it will return false.
Syntax: string_name.islower()
![]()
- Isspace Method
This method checks if string only have 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 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 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 string consists of only numeric characters. It is similar to isdigit method, 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 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 string of length specified as parameter. The original string is returned if width (length specified) is less than or equal to length of 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 string into multiple strings on arrival of character specified. If character is not found in the string then it will return 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 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 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 its 0.
3. end: Ending index position in parent string. By default its length of 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 its 0.
3. end: Ending index position in parent string. By default its length of string.
Syntax: string_name.rindex(sub, start, end)
![]()
- Rjust Method
This method is used for right justification in string of length specified as parameter. The original string is returned if width is less than or equal to length of 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 string specified.
Syntax: len(string_name)
![]()
- Max Method
This method returns the maximum alphabet character from 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 list of all sub-strings in the input string specified, using delimiter string used as separator. By default, if no delimiter is specified then whitespace is considered as 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 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 starting and ending of inputs string specified. It consists of one parameter and it is explained below:
1. char: Character to be removed from starting and ending of 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 capitalised.
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 is part of class bytes since Python 3.0 and later, earlier it was part of 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 input string specified with zeros on the left according to width parameter defined. It consists of one parameter and it is explained below:
1. width: Final width of output string
Syntax: string_name.zfill(width)
![]()