Friday, October 16, 2015

Python Tutorial: Python String Formatting Method - Python Strings #76



To Learn More Visit our website at http://learnpythontutorial.com/python-string-formatting-method/



Python String Formatting Method

Python String Formatting Method

In the previous Python tutorial, we looked at the old way to format strings in Python in this tutorial we will explore the new preferred Python string formatting method. The Python string formatting method gives us better control over how we can modify string objects. The old way has some limitations to how string objects can be modified.



When formatting a string in Python using the format method we use curly brackets({}) for placeholders versus the old way where we would use the percentage or modulo symbol(%). If you need to use curly brackets in your string itself and you do not want it to be a placeholder then we can utilize two curly brackets to escape the brackets.



Python String Formatting Method Syntax

'String Object {placeholder}.format(arguments)



'String Object {placeholder}' - This our string object that will be modified by the format string method.  The {placeholder} is where we are able to insert content into the string object to create a new string object that contains the new content.

.format(arguments) - The format string method allows us to modify string objects to create a new string object with some inserted content.  The arguments are the content that is going to replace the placeholders in the original string object.

Examples of The Python String Formatting Method

Format String Method - Example 1

#Example 1

 'This is a {} in Python'.format('string')

'This is a string in Python'

Example 1 Explained



'This is a {} in Python'.format('string') - In this example, we set up a string object that contains a placeholder({}). The placeholder is where we are about place our argument. The format string method allows us to insert the content(argument) into our string object which will return us a new string object. The 'string' is our argument which will replace the curly brackets.



'This is a string in Python' - We are returned a new string object that contains our argument in the same position that our placeholder was in the previous string object.



Format String Method - Example 2

 'This is our {} in {}'.format('string', 'Python')

'This is our string in Python'

Example 2 Explained



'This is our {} in {}'.format('string', 'Python') - In this example, we create a new string object that contains two placeholders which will be replaced by our arguments which are contained in the right side of the format string method. When adding more than two arguments we must separate them with a comma. In this case when we run this line of code Python's format string method will take the first argument and place in the first placeholder position and then the second argument and place it in the second placeholder position.



'This is our string in Python' - We are returned a new string object with the first and second place holders replaced by the first and second arguments.



Format String Method - Example 3

 'This is our {1} in {0}'.format('Python', 'string')

'This is our string in Python'

Example 3 Explained



'This is our {1} in {0}'.format('Python', 'string') - In this example, we create a new string object which is similar to example 2 but notice we place a number 1 in the first place holder and a number 0 in the second placeholder.  We also change the order of the arguments in the format string method.  When this line is ran Python will look at the number contained in the placeholders which is actually index position of the arguments. It will then locate the argument via the index position and insert it into the new string object.



'This is our string in Python' - As you can see the format string method found the first index position which is our second argument and inserted that in our new string object. Then the format string method found our next argument at the 0 index position.



Format String Method - Example 4

'We are learning {language} {version}'.format(language='Python', version='3')

'We are learning Python 3'

Example 4 Explained



'We are learning {language} {version}'.format(language='Python', version='3') - We create a string object that contains our placeholders which are now represented by curly brackets and names. These names will be used to match the arguments with your place holders.

'We are learning Python 3' - We are returned a new string object that replaces the placeholders with the appropriate arguments.

Format String Method - Example 5

 language = ('Python', '3')

'We are learning {0[0]} {0[1]}'.format(language)

'We are learning Python 3'

language = ('Python', '3') - We create a tuple that contains two values. Then we assign a variable to represent the tuple.

'We are learning {0[0]} {0[1]}'.format(language) - We create a new string object and provide two placeholders. First number indicates the index position of the argument in the format string method.

No comments:

Post a Comment