Formatting strings

We can use the format() method to format a string in Python. This method is very flexible and powerful when displaying output in any particular format. The format() method holds curly braces {} as placeholders that can be replaced by any particular arguments according to a specific order. Have a look at these next examples.

Let's first see an example for a default order:

# Default order 
String1 = "{} {} {}".format('Exploratory ', 'Data ', 'Analysis')
print("Print String in default order: ")
print(String1)

The output of the preceding code is as follows:

Print String in default order:
Exploratory Data Analysis

In addition to this default order, we can use positional formatting. For example, you have a string such as ('Exploratory', 'Data', 'Analysis'), and we want to display a ('Data', 'Exploratory', 'Analysis') string. We can do this by using positional formatting, as shown in the following example:

# Positional Formatting 
String1 = "{1} {0} {2}".format('Exploratory', 'Data', 'Analysis')
print(" Print String in Positional order: ")
print(String1)

We can also format any string by using keywords. For example, have a look at the following code:

# Keyword Formatting 
String1 = "{l} {f} {g}".format(g = 'Exploratory', f = 'Data', l = 'Analysis')
print(" Print String in order of Keywords: ")
print(String1)

The output of the preceding code is as follows:

Print String in order of Keywords: 
Analysis Data Exploratory

Next, we are going to look at how we can load a text dataset and perform preprocessing operations. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.42.116