Python string formatting with examples

In this python tutorial, we will discuss Python string formatting and also we will cover these below topics:

  • Python string formatting
  • Python string formatting multiple values
  • Python string formatting padding
  • Python string formatting padding with right alignment
  • Python string formatting padding with center alignment
  • Python string formatting padding with center alignment and * padding character
  • Python string formatting using f
  • Python string formatting float
  • Python string format methods
  • Python string formatting decimal places
  • Python string formatting integer
  • Python string uppercase
  • Python capitalize each word in a string

Python string formatting

Here, we will see python string formatting with an example.

The Python string format() method allows the formatting of the selected parts of a string. We use format to make sure that the string will display as expected.

To control such value, add a placeholder curly bracket {} in the text and then run the values through the format() method.

Example:

roll = 101
msg = "The roll start from {} "
print(msg.format(roll))

To get the output, I have used print(msg.format(roll)). You can refer to the below screenshot for the output.

Python string formatting
Python string formatting

This is how we can do string formatting in Python.

You may also like, How to concatenate strings in python? and How to add two variables in Python.

Python string formatting multiple values

Now, we will see python string formatting multiple values.

In this example, we will use multiple values in order to add multiple values to the format() method we just have to add a placeholder curly bracket {} in the text and then run the values through the format() method.

Example:

roll = 1
sub_code = 121
msg = "The roll {} has taken subject math and the code is {}."
print(msg.format(roll, sub_code))

To get the output, I have used print(msg.format(roll, sub_code)). You can refer to the below screenshot for the output.

Python string formatting multiple values
Python string formatting multiple values

Also you may like How to Convert Python string to byte array with Examples.

Python string formatting padding

Lets see python string formatting padding.

Python string can be formatted in a similar way with format(). Here, string padding with left alignment is done, and the value can be padded to a specific length. The padding character can be space or a specified character.

Example:

print("{:10}".format("Welcome"))

To get the output, I have used print(“{:10}”.format(“Welcome”)). You can refer to the below screenshot for the output of string formatting padding in Python.

Python string formatting padding
Python string formatting padding

You may also like, How to convert a String to DateTime in Python

Python string formatting padding with right alignment

Now, we will see python string formatting padding with right alignment.

In python string formatting padding with right alignment, we will use “>” for string padding in right to remaining places.

Example:

print("{:>8}".format("Hello"))

To get the output, I have used print(“{:>8}”.format(“Hello”)). You can refer to the below screenshot for the output.

Python string formatting padding with right alignment
Python string formatting padding with right alignment

You may like Python generate random number and string.

Python string formatting padding with center alignment

Here, we will see python string formatting padding with center alignment.

To get string formatting padding with center alignment we will use “^” for the center aligned to the remaining places.

Example:

print("{:^10}".format("Python"))

To get the output, I have used print(“{:^10}”.format(“Python”)). You can refer to the below screenshot for the output.

Python string formatting padding with center alignment
Python string formatting padding with center alignment

This is how to do Python string formatting padding with center alignment.

Check out String methods in Python with examples.

Python string formatting padding with center alignment and * padding character

Now, we will see string formatting padding with center alignment and * padding character.

To get string formatting padding with center alignment and * padding character we will use “*” for padding the remaining space and “^” is used for the center aligned.

Example:

print("{:*^10}".format("Guides"))

To get the output, I have used print(“{:*^10}”.format(“Guides”)). You can refer to the below screenshot for the output.

Python string formatting padding with center alignment and * padding character
Python string formatting padding with center alignment and * padding character

Check out Python write String to a file.

Python string formatting using f

Here, we will see python string formatting using f.

To create a string formatting using f, we have prefix the string with the letter “f”. The string itself can be formatted in the same way like we used str.format(). F-string is a convenient way for formatting.

Example:

value = 'Python'
print(f"Hello, Welcome to {value} tutorial {value} is very interesting.")

To get the output, I have used print(f”Hello, Welcome to {value} tutorial {value} is very interesting.”). You can refer to the below screenshot for the output.

Python string formatting using f
Python string formatting using f

This is how to do string formatting using f in Python.

Check out Remove character from string Python.

Python string formatting float

Now, we will see python string formatting float.

In the python string formatting float, we have used “{ }” as a variable inside the formatting brackets. In this example, the precision variable control how many decimal places to show.

pi = 3.1415926
precision = 2
print( "{:.{}f}".format( pi, precision ))

To get the output, I have used print(“{:.{}f}”.format( pi, precision)). You can refer to the below screenshot for the output.

Python string formatting float
Python string formatting float

Check out How to handle indexerror: string index out of range in Python

Python string format methods

Now, we will see python string format methods.

The format() method formats the specified value and insert them inside the string placeholder. The placeholder is defined by using curly brackets “{ }” and the price is fixed, with two decimal format.

val = "Its only {price:.2f} Rupees!"
print(val.format(price = 84))

To get the output, I have used print(val.format(price = 84)). You can refer to the below screenshot for the output.

Python string format methods
Python string format methods

Python string formatting decimal places

Here, we will see python string formatting decimal places.

In python string formatting decimal places, we have used “{:.3f}” to format a float to 3 decimal places and by using the format() it will get formatted.

Example:

print("{:.3f}".format(2.123456))

To get the output, I have used print(“{:.3f}”.format(2.123456)). You can refer to the below screenshot for the output.

Python string formatting decimal places
Python string formatting decimal places

This is how to do string formatting decimal places in Python.

Check out Python convert list to string.

Python string formatting integer

Now, we will see python string formatting integer.

In string formatting integer, we will use “d” which is used for integer argument. The placeholder is defined by using curly brackets “{ }”.

Example:

print("My marks is {:d} in maths".format(80))

To get the output, I have used print(“My marks is {:d} in maths”.format(80)). You can refer to the below screenshot for the output.

Python string formatting integer
Python string formatting integer

Python string uppercase

Here is another Python string formatting example.

  • Using the Python string upper() method we can convert all lowercase characters of a string into uppercase.
  • To convert any string in uppercase we have built-in upper() method in Python which will convert lowercase to uppercase.
  • The function can take any lowercase string as an input and it can produce a string in uppercase as an output.

Example:

  • First of all, We need to take a string value. In the below python code, You can see, I have taken the value as “welcome to python”.
  • Now print the “msg.upper()” and it will return the string in uppercase letter.

Python string uppercase Code

Below represents the python code for string uppercase.

msg = "welcome to python"
print(msg.upper())
  • After writing the above Python code (string uppercase), Ones you will print “msg.uppercase()” then the output will appear as a “WELCOME TO PYTHON” which will return a uppercase string as an output. Also, you can refer to the below screenshot.
Python string function uppercase
Python string uppercase

This is how to convert a string to uppercase in Python.

Python capitalize each word in a string

In python, to capitalize each word in a string we will use the built-in method upper() to capitalize all the letters in the string.

Example:

my_string = 'python guides'
print(my_string.upper())

After writing the above Python code (python capitalize each word in a string), Ones you will print “my_string.upper()” then the output will appear ” PYTHON GUIDES “. Here, the string.upper() is used to capitalize each word in the string. Also, you can refer to the below screenshot to capitalize each word in a string.

Python capitalize each word in a string
Python capitalize each word in a string

This is another Python string formatting example, where we saw how to capitalize each word in a string in Python.

You may like the following Python tutorials:

In this tutorial, we have learned about Python string formatting, and also we have covered these topics:

  • Python string formatting
  • Python string formatting multiple values
  • Python string formatting padding
  • Python string formatting padding with left alignment
  • Python string formatting padding with right alignment
  • Python string formatting padding with center alignment
  • Python string formatting padding with center alignment and * padding character
  • Python string formatting using f
  • Python string formatting float
  • Python string format methods
  • Python string formatting decimal places
  • Python string formatting integer
  • Python string uppercase
  • Python capitalize each word in a string