This is Part 4 of the Mastering Python for Blockchain series. It will explain Strings in Python-Functions used  . In fact, this is Chapter 2 of Strings. Chapter 1 of Strings is here.

I will suggest you to download the latest version of Python which will help you to practice coding and compiling programs. In case you do not have Python installed in your system you can use this link to online run your Python codes.

I also suggest you to visit the article here to learn the basic concepts of Blockchain.

Note: Generally for functions used in Strings, the format of writing in the code is string name.function(argument)

Note: Strings are immutable in Python. When you apply a function on a string it creates a new string with the modified value.

Strings in Python

Strings are the first example in Python sequences.

Strings are a sequence of characters. Characters is the smallest unit in the writing system. It can be letters, digits, symbols, punctuation and even white spaces and directives like linefeeds.

For more details, please refer to Part 3 of the Mastering Python for Blockchain Series.

Strings in Python-Functions used

Substitute with replace( )

  • We can replace a substring by using the replace ( ) function.
  • As an argument give it the old substring, then the new one and then how many instances of the old substring to replace.
  • Format:
    string.replace(‘old substring’,’new substring’,no. of instances of the old substring to replace )
  • Output:

a)Please note that the substring ‘dog’ is replaced with the substring ‘cow’.
b) There is no count of the number of instances of the substring ‘dog’ to be replaces is specified. This will prompt Python to replace all instances of the substring ‘dog’ with the new substring ‘cow’. Since there is only one instance of ‘dog’, it is replaced once by the new substring ‘cow’.
c)Moreover, please note that replace( ) returns or prints the changed string, but it does not modify the original string. This means that a new string is created in the computer memory with the changed substring. But the old string remains as is in the computer memory.

Output:

  • Now check the below example.

Output:

In the above example, since again we have not specified the count of the number of instances of substring ‘dog’ to be replaced, Python replaces all the instances. If however we specified the count then Python will replace only till the count specified. See the below example.

Output:

Now try the below code.

In this one, Python will replace two instances of the substring ‘a’.

Output:

If we remove the count of instances, Python, as said earlier, will replace every instance of the substring ‘a’. See below.

Output:

What should be the output of the below code.

Since we are now replacing the double characters a and space it will replace only the a with spaces.
Output:

Strip or remove with strip( )

  • It is common that we want to get rid of whitespace characters like (‘ ‘, ‘\t’, ‘\n’)
  • strip( ) function helps you to do so. It removes all the whitespace characters from the string.

Output:

  • lstrip( ) removes the whitespace characters from left. rstrip( ) removes whitespace characters from the right of the string.
  • When there is no argument defined for the strip( ), it removes the whitespaces. Whereas, we also can remove any character/characters in a multicharacter string.

Output:

Functions used for Search and Select in Python Strings

Python has a large set of string functions to search and select in a string. Let us check them one by one.

Let us take our test string be as shown below. We will use this string as our reference for this entire ‘Search and Select in Python Strings’.

  • Let us first try and find out the first 12 characters of the string.

This will find out the first 12 characters of the string which will include blank spaces also.

Output:

  • To find the number of characters in a string: len( ) function

Output:

107

Again, this will also contain the spaces and newlines.

  • To find whether the string starts with a specific substring: startswith ( ) function

Output:

Output:

  • To find whether a string ends with a specific substring: endswith( ) function

Output:

But what happens when we check with sky rather than sky; (with the 😉

Output:

  • To find the offset of a substring: find( ) and index( ) functions

We can use both the find ( ) and index ( ) functions to find the offset or the location of a substring or a character in a string.

Output:

Some more to find characters.

Output:

Note: The offset starts from 0 to 1,2,3,………………………..

  • To find the number of times a substring or character occurs in a string: count( ) function

Output:

Output:

Some more in-built functions in Strings

Let us take the same string we used earlier, as shown below.

  • Capitalize the first letter of the first word: capitalize( ) function

Output:

  • Capitalize the first letter of all the words of the string: title ( ) function
  • Capitalize all the letters of the string: upper( ) function

Output:

  • Convert all the characters of the string to lowercase: lower( ) function

Output:

  • Swap uppercase letters with lowercase letters and vice versa using the swapcase( ) function
    Let us now consider a string as shown below.

Output:

Alignment functions for strings in Python

Consider the string shown below.

Using the len( ) function we can find out the number of characters in the string.

Output:

29

We use center( ) for center alignment, ljust( ) for left alignment and rjust( ) for right alignment

1 Comment

Leave a Reply