Python String

Python string is considered as the most popular data type present in Python language. In Python, string data type is the collection of characters, letters, white spaces etc. written inside single, double or triple quotation marks.

  • The computer does not understand the characters that we write in our program. Internally, the character or string is encoded in binary language i.e., in the combination of 0s and 1s.
  • Each character that we type on computer is encoded with the ASCII or Unicode character encoding in the computer internally.
  • So that we can also say that Python string are also the collection of Unicode character encoding system.
  • We can create string in our Python program by enclosing the sequence of characters or words in the quotation marks (single, double or triple).
  • The string written inside single, double or triple quotation marks in Python is considered same.

Example: ‘Hello Python Developers’, “Hello Python Developers” and ‘”Hello Python Developers”‘ all three are considered same string in Python.

Syntax of Python String

Using the following syntax, we can define a string in our Python program:

The string1 variable holds the group of characters in quotation marks that is a string basically, so when we will check the data type of string1 it will show us string type.

To check the data type of string1 variable, we have to write the following syntax:

Note: In Python, group of characters are also considered as string data type. This is because Python don’t have character data type. A single character like ‘M’ written in Python program will be considered as string data type with length 1.

Creating a String in Python:

As we know, we can define strings by enclosing in single, double or triple quotation marks. But generally, we don’t use triple quotation marks to define string in Python program. Triple quotation marks are usually used for defining multi-line strings that is called as docstrings in our Python program.

Example

Output:

Creating a Multiline String in Python:

We can define a multiline string in our Python program using the triple quotation marks only. Let’s understand this through an example.

Example:

Output:

Explanation:

In the above code, we have defined a multiline string mkr. We can define a multiline string in Python by adding two extra quotation marks before initiating string with triple quotation marks.

We don’t need to add those two extra quotation marks while ending the string. When we print our multiline string i.e., mkr, Python will print the string output in exact number of line that we have used while defining the string. The data type of multiline string defined by us will also be same in Python i.e., string data type.

Indexing string in Python:

Like any other programming languages, the indexing in Python string begins from 0. The first element or character present in a Python string will be denoted by 0th index.

Let us understand this through an example.

Example: Look at the following string “PYTHON” is indexed in the below code:

This is the indexing of string mkr which we defined above and that’s how string elements are indexed in Python program.

Using the index of a string we can print a specific element present inside a string. Consider the following example with the same string mkr:

Output:

Explanation:

In the above code, we have defined an index mkr. Then we printed each element of mkr string one by one using the indexes of the element. After that, we have tried to print the element of whose index not exists in string mkr. Python will show us an index error after running the program.

Negative Indexing of the element:

In Python, we can also print the element by using negative indexes. The negative index starts from last element and denoted by -1 index. Negative index is very helpful in Python strings when we have to print elements from last and we don’t know the length of the string.

The -1 index represents last element of a string, -2 represents second last element of that string and so on.

Look at the following example with the string mkr:

Using these negative indexes, we can easily call the elements from last of a string.

Example: Look at the following code for how can we print elements from last using the negative indexes:

Output:

Explanation:

In the above code, we have defined a string mkr with some elements in it. Then we printed the elements from the last using the negative indexes of elements.

The negative Indexing is very helpful when we don’t know the exact length of string. We can easily print elements from last using negative indexes without even knowing exact length of the string.

Slicing in Python string:

A slicing operator is used to slice the elements and print a specific number of elements from a string. In Python, slice operator is denoted by []. As we have seen above in indexing of string, how can we access the specific element of a string using this slice operator.

Now, we will learn how to access more than one element from a string using the slice operator. For this, we will use the colon (:) operator inside the slice operator [] to access a substring from the given string.

Let’s understand this through an example.

Example:

Output:

Explanation:

 In the above code, we have defined a string mkr with some elements in it. Then we have used the colon operator (:) inside the slice operator ([]) to print the substring from string mkr. Then, we have printed the substring using the indexes of elements from mkr string.

Let’s look at another example.

Example:

Output:

Negative Slicing of String:

We can also perform the slice operation on Python strings using the negative index. The negative indexing in string starts from the rightmost character present in the string. In the negative slicing, we have to write negative indexes of string inside the slice operator and between colon operator to print the substring.

Let’s understand this through an example.

Example 1:

Output:

Let’s look at another example of negative slicing.

Example 2:

Output:

Explanation:

In the above code, first of all we defined string1 with elements. Then, we printed the substring from string using the negative indexes using the slice operator. Then, we printed the whole string using only one negative index inside slice operator. In the last line of code, Python will show us index because the index is out of the range from string1.

Reassigning the Python strings:

In Python, the string object doesn’t support assignment of new items i.e., The content or elements of a string cannot be partially replaced with new elements. We can replace the content of a defined string with completely new content and the new content can be of different size.

  • Updating the content/elements present in a Python string is simple as defining a new string.
  • Python strings are Immutable in nature i.e., we cannot perform string operations on partial elements of string.

Explanation: we cannot partially update the content or elements of a string.

However, we can completely update or replace the content of string mkr to a completely new content from above example.

Example 2: Look at the following code:

Output:

Explanation: In the above code, we have updated mkr string completely by replacing the content of it by a whole new content. Then we printed the string mkr.

Deleting the String:

As Python strings are immutable in nature, we cannot perform delete operation on partial elements of a string. We cannot delete or remove some specific characters from a string. If we try to delete the specific characters from a string, Python will through as an error.

Let’s understand this through an example.

Example 1:

Output:

Explanation:

When we used del keyword on mkr string, Python through us an error in the output. This is because Python doesn’t support delete operation on particular characters of a string.

However, we can delete the complete string from Python.

We can perform delete operation on string and delete it completely from Python memory. We can perform delete operation on Python string using the del keyword on string.

Let’s try to delete the entire string mkr from the above example.

Example 2:

Output:

Explanation:

In the above code, we have deleted the entire string mkr from Python memory using del keyword. After that, we have tried to print string mkr and Python will throw an error because no such item is now present in the Python memory.

Operators for Python string

We can perform various operations on string using the given operators in Python. These string operators can concatenate, multiply and do many other operations with string. Following are the list of string operators present in Python:

1. Concatenate operator (+)

2. Repetition operator (*)

3. Slice operator ([])

4. Slice operator with colon ([:])

5. Membership operator (‘in’ and ‘not in’)

6. Raw string operator (r/R)

7. String formatting operator (%)

Let’s discuss about each of the string of the string operator mentioned above.

1. + Operator (concatenate operator):

The ‘+’ denotes the concatenate operator and perform concatenation operation on strings in Python. The concatenate operator is used to add two or more strings.

We can use the concatenate operator in two following ways:

  • Add two strings and assign its value to a third string
  • Concatenate two string while printing the output

Let’s understand this through an example.

Example 1:

Output:

Explanation:

As we can see, we have defined two strings then we have added them and assigned the value to third string i.e., mkr. Then we printed the mkr string. The mkr string holds the concatenated value of two strings.

Example 2:

Output:

2. Repetition operator (*):

The ‘*’ symbol denotes the repetition operator in Python and it repeats the strings. The repetition operator is used to repeat the string value. We can also assign the repetition value of a string to another string.

Example: Look at the following code:

Output:

Hello Python DevelopersHello Python DevelopersHello Python DevelopersHello Python Developers

Explanation: In the above code, we have defined a string mkr and used repetition operator on it. When we used repetition operator on string while printed, Python printed the mkr string 4 times as we have given in code.

3. Slice operator ([]):

As we have discussed earlier about slice operator, it is used for slicing of a string. The close square brackets ‘[]’ represents slice operator in Python. Slice operator is used to print a particular element from a given string. For that we need to specify the index of the element which we want to print in output.

Example: Look at the following code:

Output:

4. Slice operator with colon ([:]):

The colon is used inside a slice operator to print substring from a given string. We can also use negative indexes inside the slice operator while printing substring with colon.

Example: Look at the following code:

Output:

5. Membership operator (in and not in):

We can check the membership of a substring from a string using the membership operator. The membership operators are used to check if a particular substring belongs to the string defined in our program or not. Membership operators are of two types:

(i) ‘in’ membership operator

(ii) ‘not in’ membership operator

Let’s discuss about both types of membership operator with one example of each.

(i) ‘in’ membership operator: The ‘in’ operator is a type of membership operator. It is used to check if a particular substring is present in the string defined in our program. The output given by ‘in’ operator is same of boolean operator. It gives output as either true or false.

Syntax for ‘in’ operator:

Example: Look at the following code:

Output:

(ii) ‘not in’ membership operator: The ‘not in’ operator is also a type of membership operator. It is used to check if a particular substring is not present in the string defined in our program. The output given by ‘not in’ operator is also same of boolean operator. It gives output as either true or false.

The ‘not in’ operator always gives opposite output of ‘in’ operator i.e., If ‘not in’ gives False then ‘in’ operator will give true in output or vice-versa.

Syntax for ‘not in’ operator:

Example: Look at the following code:

Output:

6. Raw string operator (r/R):

The raw string operator is used to specify a raw string in our Python program. Raw strings are the strings where we need to print actual or as it is meaning of the escape characters (We will talk about escape characters present in strings later in this section) of string such as “C://Manish/Python” etc.

Syntax for raw string:

Let’s understand this through an example.

Example:

Output:

7. String formatting operator (%):

The ‘%’ symbol denotes the string formatting operator in Python. The string formatting operator is used to perform formatting operations on string. The string formatting operator makes use the format specifiers from the C programming languages such as %f or %d etc.

  • The format specifiers are used by string formatting operators to map their values in Python.
  • We will discuss about the how formatting is done through string formatting operator on Python strings.

Example: Look at the following Python program with string formatting operator:

Output:

Now, we will discuss about Python string formatting and escape characters in Python string in detail.

Python string Formatting

Escape sequence in Python string:

Let’s suppose we have to write or print the following string in our Python program:

Mike said, “Hello People, what’s going on there?”

The above statement can be written in single or double quotation marks but it will raise the syntaxerror in our program because both single and double quotes are already present in the above statement.

We can’t even use triple quotation marks with such sentences because even use of triple quotation marks with such sentences causes ‘syntaxerror’ while defining strings in our Python program.

Let’s use this sentence in a Python program to understand it better.

Example: Look at the following code to understand the concept of use of Python string operators:

Output:

We can solve this problem by using triple quotation marks in this sentence. But, use of triple quotation marks is also limited. Triple quotation marks cannot be valid with every given sentences.

Therefore, Python provides us the escape sequence which is very useful in such problems.

The backlash symbol (\) represents the escape sequence present in Python.

We can use this backlash symbol (\) with a special character and it will be interpreted differently in Python.

Different special characters used with escape sequence (\) represents different functionalities and different uses.

Escape sequence (\) escapes the interpretation of some characters, quotation marks (single, double), special characters etc. present in the given sentence.

Now, we will apply both triple quotation marks and escape sequence in the above given sentence and print it as output.

Example: Look at the following code to understand the use of escape sequence in Python program:

Output:

Explanation:

In the above code, first we printed the given sentence using the triple quotation marks. The triple quotation marks defined the string type in print statement so that single and double quotes both are printed in output without throwing an error in program.

Then, in second print line we used escaping sequence on single quote in sentence. Therefore, we have used single quotation mark while printing the statement. In the last print line, we used escape sequence on double quotation marks for escaping double quotes from sentence.

Therefore, we have used double quotation marks to define the sentence in print statement. Using escape sequence will solve the syntaxerror problem even if we define our sentence with single or double quotes.

Few Special Escape Sequence

As we have mentioned earlier, we can use some special characters with escape sequence and Python will interpret them differently. Using different special characters with escape sequences represents different functionalities of escape sequence and they are used differently according to the need of program.

Following is the list of all escape sequences in Python used with different special characters:

1. \newline

2. \\

3. \’

4. \”

5. \a

6. \b

7. \f

8. \n

9. \r

10. \t

11. \v

12. \ooo

13. \xHH

Here is an example of escape sequence in Python program and how they are used.

Example: Look at the following code:

Output:

Now, let’s look at the each of escape sequence and their use in Python program.

1. \newline: This escape sequence is used when the code is written in multiple lines. It will ignore the new or multiple lines when used with the line of code. We can use this even in our print statement and then write command in multiple lines.

Example: Look at the following code:

Output:

2. \\: This print statement is used to print backlash itself in the print statement otherwise Python will ignore the backlash while interpreting the code.

Example: Look at the following code:

Output:

3. \’: This escape sequence will print the given sentence and ignore the interpretation of single quote within the sentence. This escape sequence solves the problem of syntax error where both single and multiple quotation marks are given in a sentence.

Example: Look at the following code:

Output:

4. \”: This escape sequence will print the given sentence and will ignore the interpretation of double quotes given in the sentence.

Example: Look at the following code:

Output:

5. \a: This is a special escape sequence. It represents ASCII Bell (Bel). This sequence will print nothing but makes ringing the bell alert sound when used in Python program. (E.g., xterm etc.)

Syntax:

6. \b: This escape sequence is used to represent ASCII backspace (BS) in Python. This escape sequence removes the previous character in a sentence where it is used. As the representation suggestion, it works as a backspace within the print statement.

Example: Look at the following code:

Output:

7. \f: This escape sequence represents ASCII form feed in Python. It will add white space within the words and print them in separate pages when used in a sentence. We can use \f escape sequence in the print statement.

Example: Look at the following code:

Output:

8. \n: This escape sequence represents ASCII linefeed in Python program. It is used to add an extra line in the sentence. We can use this \n escape sequence within a sentence in the print statement to print the sentence in multiple lines as output.

Example: Look at the following code:

Output:

9. \r: This \r escape sequence moves all the characters after the beginning of the line while overriding the same number of characters that are moved within print statement. This escape sequence represents ASCII Carriege Return (CR) in Python.

Example: Look at the following code:

Output:

10. \t: This \t escape sequence is used to represents ASCII horizontal tab in Python program. When we use this escape sequence within a sentence, it will add a tab space between the words of that sentence.

Example: Look at the following code:

Output:

11. \v: This \v escape sequence is used to represents ASCII vertical tab in Python program. When we use this escape sequence within a sentence, it will add a vertical tab space between the words of that sentence. It means \v escape sequence add an extra line between the words where it is used in sentence.

Example: Look at the following code:

Output:

12. \ooo: This escape sequence is used to decode the words that are encoded in octal value format. The sentence or words are written in octal value format and \ooo escape sequence decodes them in alphabetical words and prints the output after decoding.

Let’s understand the working of \ooo escape sequence through an example.

Example:

Output:

13. \xHH: The ‘\xHH’ escape sequence works very similar to the \ooo escape sequence. This escape sequence is used to decode the characters that are encoded in the hex value formats. The sentence or words are written in hex value format and \xHH escape sequence decodes them in alphabetical words and prints the output after decoding.

Let’s understand the working of \xHH escape sequence through an example.

Example:

Output:

The String format() method:

The string format() method is the very helpful and flexible method in formatting of strings within a Python program. In the format method, we use curly braces as the placeholders inside the print statement or string and formatting is done by replacement using format() method argument.

The format() method of string in Python is very helpful when we want to modify string just before printing it in output.

Let’s look at an example of format() method argument.

Example:

Output:

Explanation:

In the above code, we have used the format() method on string within the print statement. We used curly braces as placeholders in the string and then we placed values from format i.e., “Manish Arora” & “Gaurav Panwar“. Python printed the output string after performing format() string argument on curly braces.

We can use the curly braces in two ways as placeholders in the string while using format() method argument. The two ways of using curly braces as placeholders are following:

1. Using digits inside curly braces for positional argument in format() method:

In this method, we use numerical digits inside curly braces to perform positional argument on string with format method() in print statement.

Example: Look at the following code:

Output:

2. Using alphabets inside curly braces for keyword argument in format() method:

In this method, we use alphabets value inside curly braces to perform keyword argument on string with format() method in print statement. We will assign element value to alphabets in the format() method to use them in curly braces.

Example: Look at the following code:

Output:

Formatting of Python string using the ‘%’ operator:

In Python, we are allowed to use the string format specifiers that we use in printf statement in C to format a particular string. The format specifiers that we use with strings in Python are treated or operated in the same way as they are treated in C language. The ‘%’ operator is called as format specifier in Python. We use keywords with it perform operation on Python string with format specifiers.

  • Moreover, Python provides an extra operator i.e., the ‘%‘ operator.
  • The ‘%’ operator is used as an interface between the format specifiers of Python strings and value allocated to them.
  • It means we can say that the ‘%’ operator binds format specifiers of Python strings to their values allocated.

Let’s understand the concept of ‘%’ operator through an example.

Example: Look at the following code:

Output:

Explanation: In the above code, firstly we have defined three variables with integer, float and string values respectively. Then, we printed the variables with the print statement using the % operator. The % operator assigned the variables to respective sentences in the print statement as we can see that. The % operator is also used to define the data-types of variable while assigning variables to respective sentences.

Python String Functions

Like many popular programming languages, Python also provides various built-in functions for strings. These built-in functions are used for string handling in Python. Following are the many built-in string functions that are present in Python:

capitalize()The ‘capitalize()’ function capitalize the first character of the given string. The ‘capitalize()’ function for string is deprecated in Python 3.x versions.
carefold()The ‘carefold()’ function returns the versions of s suitable for care-less comparisons between strings and assigned values.
rpartition()The ‘rpartition()’ function spilits a given Python string from the last occurrence of separator substring of given parameter. This method returns a tuple after performing split operation on the given string.
centre(width,fillchar)The ‘centre(width,fillchar)’ function returns a space padded string after performing operation on it. This method returns a space padded string that is centred with equal number of spaces on left and right of string.
zfill(width)The ‘zfill(width)’ function returns the original string on which operation is performed leftpaded with zeroes to a total width character given in the function. This function retains any sign given in tje the string (less one zero) that is intended for numbers only.
count(string name, begin, end)This ‘count()’ function is used to count the total number of occurrences of a substring in the given string inside the function. This function counts the occurrence of substring between the beginning and end index we defined in the function.
upper()The ‘upper()’ function in Python is used to convert or change all the characters of the given string in the upper case form.
decode(encoding = ‘UTF8’, errors = ‘strict’)The ‘decode(encoding = ‘UTF8’, errors = ‘strict’)’ function decodes the given string using the codec registered for encoding form of string.
translate(table,deletechars = *)The ‘translate(table,deletechars = *)’ function translates a given string according to the translation table that we have passed in the function.
encode()The ‘encode()’ function encodes the given string using the codec registered for encoding of strings in Python. The default codec registered for encoding of Python string is ‘UTF-8’.
title()The ‘title()’ function is used to convert the given string inside the function to a title-case form. The given string PaLWal will be converted to Palwal.
endswith(suffix,begin = 0,end = len(string))The ‘endswith(suffix,begin = 0,end = len(string))’ function returns a boolean value i.e., True or False if the given string in the function terminates with the suffix given between the begin and end of function value.
swapcase()The ‘swapcase()’ function is used to inverts case all the characters of the given string in the function. The string Palwal will be sorted to lawlaP in swapcase().
expandtabs(tabsize = 8)The ‘expandtabs(tabsize = 8)’ function define tha tabs in the given string to multiple spaces. The default space value inside this function is 8 for a string.
strip([chars])The ‘strip([chars])’ function is used to perform lstrip() and rstrip() function on the given string in Python program.
find(substring, beginIndex, endIndex)The ‘find(substring, beginIndex, endIndex)’ function returns the index value of the string where the given substring in the function is find in the string on which operation is performed. The operation is performed on the string from the begin Index and end Index defined inside the function.
startswith(string,begin = 0,end = length(string))The ‘startswith(string,begin = 0,end = length(string))’ function returns a boolean value if the given string starts with the ‘string’ value defined in the function between the beginning and end of index.
format(value)The ‘format(value)’ function returns the formatted version of the given string using the passed value defined in the function.
splitlines(num=string.count(‘\n’))The ‘splitlines(num=string.count(‘\n’))’ function returns the total list of string present in each line with the newline removed from the string after performing operation on it.
index(substring, beginIndex, endIndex)The ‘index(substring, beginIndex, endIndex)’ function throws an exception if the given substring is not found in the given string between the begin index and end index defined inside the function. This function works same as of ‘find(substring, beginIndex, endIndex)’ function.
split(str, num = string.count(str))The ‘split(str, num = string.count(str))’ function splits the given string according to the delimiter str we have defined within the function. The string is splited according to the space in string if the delimiter is not provided in the function. This function returns list of substrings concatenated with the delimiter provided inside the function.
isalnum()The ‘isalnum()’ function return true after performing operation if the given characters in the string are in the form of alphanumeric values. There should be alteast one character in string for the function to return true in output. Otherwise, in all other cases, this function will return false in output.
rsplit(sep = None, maxsplit = -1)The ‘rsplit(sep = None, maxsplit = -1)’ function works in the same process as of ‘split(str, num = string.count(str))’ function but this function process the given string from the backward direction. This function returns the list of total words present in the given string. If the separator is not specified within the function, then the function splits the string according to the white-space present in the string.
isalpha()The ‘isalpha()’ function returns true in output only if all the characters present in the given string are alphabets and there should be at least one character is present in the string. Otherwise, this function will return false in the output.
rstrip()The ‘rstrip()’ function removes all the trailing white spaces present in the given string. This function is also used to remove a particular character in trailing of the given string.
isdecimal()The ‘isdecimal()’ function returns true in the output if all the characters present inside the given string are decimals. Otherwise, this function will return false in output.
rjust(width,[fillchar])The ‘rjust(width,[fillchar])’ function returns a space padded string in the output if having the original string right justified to the total numbers of characters specified in the given original string.
isdigit()The ‘isdigit()’ function returns true in the output if all the characters present in the given string are digits and there should be atleast one character is present in the string. Otherwise, this function will return false in the output.
rindex(str,begin = 0,end = length(str))The ‘rindex(str,begin = 0,end = length(str))’ function works same as of index() function but this function also transverse the given string in the backward direction in the output.
isidentifier()The ‘isidentifier()’ function returns true in the output if the given string in the function is a valid identifier.
rfind(str,begin = 0,end = length(str))The ‘rfind(str,begin = 0,end = length(str))’ function works very similar to the find() function works on string but this function transverse the given string in backward direction after performing the operation on it.
islower()The ‘islower()’ function returns true in the output if all the characters present in the given string are in lower case form ,otherwise it will return false in the output.
replace(old,new[,count])The ‘replace(old,new[,count])’ function replaces the old sequence of character present in the given string to the new sequence of characters defined in function. The maximum character are replaced from the string if max is given in the function.
isnumeric()The ‘isnumeric()’ function returns true in output only if the given string contains all the characters are in numeric form otherwise, this function will print false in output.
maketrans()The ‘maketrans()’ function return a translation table in the output according to the given string that to be used in the translate function.
isprintable()The ‘isprintable()’ function return true in output if all the characters of given string in function are printable or the given string is empty, otherwise this function will print false in output.
partition()The ‘partition()’ function searches for the separator sep in the given string ‘S’ inside the function and returns the part of the string before the separator, the separator ‘sep’ itself and the part after the separator in output. If the separator ‘sep’ is not found in the given string then this function will return the original string S and two empty strings in the output.
isupper()The ‘isupper()’ function returns true in output if all the characters of the given string in function are in the upper-case form, otherwise this function will return false in output.
Istrip()The ‘Istrip()’ function removes all the leading white spaces present in the given string in function. This function is also used to remove a particular character in the given string from leading.
isspace()The ‘isspace()’ function returns true in output if the given string in function only contains white spaces in it, otherwise this function will return false in output.
istitle()The ‘istitle()’ function return true in output only if the given string in function is in proper titled form, otherwise this function will return false in output. This function will return true for Palwal string but false for all other form of it (palwal, PALWAL etc.)
lower()The ‘lower()’ function is used to change all the characters of the given string in function in the lower case form.
upper()The ‘upper()’ function is used to change all the characters of the given string in function in the upper case form.
join(seq)The ‘join(seq)’ function used to merge the given string’s representation in the given sequence provided in the function.
Ijust(width[,fillchar])The ‘Ijust(width[,fillchar])’ function returns the space padded string in the output with the original string given is left justified to the given width provided in the function.
len(string)The ‘len(string)’ function returns the length of the given string provided in the function in the output.

Pin It on Pinterest

Share This