Introduction

This is Part 3 of the Mastering Python for Blockchain series. It will explain strings in Python, operators and functions used on strings . But before you delve into this Part 3 of the Mastering Python for Blockchain series please visit Part 1 and Part 2 of this Mastering Python for Blockchain series.

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.

Strings–>Mastering Python for Blockchain

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.

  • Strings in Python are made by enclosing characters in matching single or double quotes.
    Ex: ‘example’; ‘python’; “great”
  • Why do we have two kinds of quotes for strings in Python? This is to create quoted characters in the output.
    See below.

The outputs are:

  • We can also use triple single strings or triple double strings. See below.

Output:

  • Triple quotes are generally used with multi-line strings. See the example below.

Output:

Try with single or double quotes.

You will get an error like the below.

Here we could have used the line separator \. See below.

Output:

As you can see, the Output comes in one single line instead of multi-lines as in the previous example. So use triple single or triple double quotes for printing multi-line outputs.

  • Empty Strings: There are empty strings also which do not have any characters but only single, double of triple quotes.
    Ex:

Output:
blank as below.

String function str()

  • str() function is used to convert a different data type, example integer to the string data type.

Output:

Operators used on Strings–>Mastering Python for Blockchain

Escape with \

  • In Python using \(backlash) with certain characters escapes the meaning of those characters. We will see how using \ with n, t,”, even \ changes their meanings.
  • \n: In Python, when we place \(backlash) with the letter n it prompts the interpreter to start a new line. \n should be placed just before the string to be put in the next line.

Output:

\n will not work in case it is not placed right next to the string to be put in the next line.

Output:

  • \t: In Python we use \ with the letter to apply tab as is used in your computer or laptop. See below.

Output:

  • \ with “:

Output:

Combine with +

  • You can add two or more variables by using the + operator. See below example.

Output:

Or you could have done it like,

Output:

  • See Python doesn’t give you spaces between the literal string in the output.
  • Though Python will add a space between each argument of the print( ) statement. See below.

Output:

Duplicate with *

  • Use the * operator to duplicate a string.

Output:

Also see below.

Output:

Get a character with [ ]

  • Consider the string specified below.
    string=’aeioujklmnop’
  • What should we do if we want to get a single character of the above string as output.
  • Pleas note that in Python strings, the characters starting from the left are denoted by offsets 0 then 1 then 2 then 3 and so on.

Output:

  • Now in order to avoid counting long, the rightmost character is specified by offsets -1, going left is specified by -2 then -3 and so on. This is specifically taken to account when we are dealing with long strings.
    letters=’abcdefghijklmnopqrstuvwxyz’

Output:

Getting a Sub-String with a Slice

  • We can get a sub-string from the string by using a slice.
  • A slice is defined by using square brackets, a start offset and an end offset, and an optional step count between them.
  • The slice will include characters specified by the start offset to the character specified by end offset minus 1

Output:

  • [:] extracts the entire sequence from beginning to end
  • Output:
  • What happens for [20:]-this will print from offset 20 till the end

Output:

[:end] extracts all the characters from start to end minus 1

Output:

  • Again [start:end] will extract from start to end minus 1.

Output:

  • [start:end:step] extracts from the start offset to the end offset minus 1, skipping characters by step.

This will print from a to y skipping characters by 3

Functions used on Strings–>Mastering Python for Blockchain

Till now we have seen some punctuation symbols like +, \,* which have their specific roles in Python. Now we will see some built-in functions in Python, which have some pre-defined roles. Functions are pre-defined codes in Python which have some specific roles.

Get length with len( )

  • The len( ) function determines the length of a string. In fact len( ) can be used with other sequence types also

Output:

Output:

Split with split( )

  • The split( ) function is used to split a long string into a list of smaller strings, separated by commas and surrounded by square brackets.
  • The format of using the split ( ) function is string name.split (argument)
  • In the below example, long_string is the name of the string. The argument we use is ‘, ‘.
    This calls the split ( ) function to split the long_string from wherever comma (,) appears.

Output:

  • If however we did not specify any argument for string ( ) function, then it would have split the long_string from wherever it saw blank spaces, tabs or newlines. See below.

Output:

Join a list of strings using join( )

  • We can join a list of strings using the join ( ) function.
  • Here you need to specify the string that glues everything together first, and then the list of strings to glue: string .join( list ).
  • See below.

Output:

We will cover more functions used in strings in Part 4 of the Mastering Python for Blockchain series. Till then practice the operators and functions discussed in this part 3.

2 Comments

Leave a Reply