Python String Formatting

String Formatting lets you inject items into the string rather than chain items together using commas or string concatenation

Way of string formatting

The oldest method involves placeholders using the modulo % character
An improved technique uses the .format() string method
The newest method, introduced with python 3.6 uses formatted string literals called f string

Please read the related topic for more understanding of Python
Keywords and Identifiers
Variables and Data Types
Number in Python
Python String

String Formatting placeholders

print("I'm going to inject %s text here, and %s text here." %('some', 'more'))

Output-

I'm going to inject some text here, and more text here.

Format conversion method:

print('I am going to %s.' %'this \tbig City')
print('I am going to  %r.' %'this \tbig City')

Output-

I am going to this 	big City.
I am going to  'this \tbig City'.

String Formatting with the format() method

print('{2} {1} {0}'.format('Dzire', 'Dreams', 'Neil'))

Output-

Neil Dreams Dzire


Place holder vs Format

print('Hello %s.' %('World'))

print('Hello {p}.'.format(p='World'))

Output-

Hello World.
Hello World.

Format: Inserted objects can be reused, avoiding duplication

Formatted string literals (F-String):-

name = 'Dreams'
print(f"Live for {name}.")

Output-

Live for Dreams.
num = 23.45687

print(f'Decimal number is: {num:{10}.{6}}')

Output-

Decimal number is:    23.4569

Built In Function

string.ascii_letter
Concatenation of the ascii-lowercase and ascii-uppercase constants.

string.ascii_lowcase
Concatenation of lower case letters

string.ascii_uppercase
concatenation of uppercase letters

string.digits
digits in string

string.hexdigits 
Haxdigit in string

string.letter
concatenation
of lowercase and uppercase

string.endswith()
Return true if a string ends with the given suffix otherwise return false

string.startwith()
Return true if a string starting with the given suffix otherwise return false

string.swapcase()
method converts all uppercase to lowercase and vice versa of the given string and returns it

replace()
return copy of the string where all occurrence of a substring is replaced with another substring.

string.isdecimal
return true if all the characters in a string are decimal
Similarly other method like Isalnum, Istitle Identifier

string.partition
Splits the string at the first occurrence of the separator and returns a tuple

string.len
Return length of string

string.rindex
Return the highest index of the sub-string inside the string if the substring is found

string.max
Return the highest alphabetical character in string

string.min
Return the minimum alphabetical character in string

string.splitlines
Return a list of lines in string

string.capitalize
return a word with its first characters capitalized

string.find
Return the lowest index in the substring

string.count
Return the number of occurrences of a substring in string

string.lower
Return a copy of string but convert to lowercase

string.split
Return a list of the word of string

string.ljust
left-justify in a field of given width

string.rjust and string.center for right justify and center jusitfy

Please download the example of String Formatting in Python

Leave a Comment

Your email address will not be published. Required fields are marked *