Learn Python Tutorial is the leading Python tutorial website with hundreds of Python tutorials. If you want to learn Python then this site is for you. Check out learn Python tutorial today! http://learnpythontutorial.com
Wednesday, October 14, 2015
Python Tutorial: Python String Formatting The Old Way - Python Strings #75
Learn More Visit our site at http://learnpythontutorial.com/python-string-formatting-the-old-way/
Python String Formatting The Old Way
In this Python tutorial we are going to talk about Python string formatting the old way. There are two ways to format a string in Python. The old way of using a modulo which is sometimes referred to the printf style of formatting. We focus on the printf style in this tutorial. Python has mentioned several times over the last several years that they would remove this method of formatting strings. This method is yet to be yanked do to the fact that so many programs still use this method. The preferred method to formatting strings in Python is to the format string method. We will discuss the format string method in the next tutorial.
Formatting a string in Python allows us to add content to existing string object which will return us a new string object. We use string formatting often to modify content that will be displayed to a user. For example, we would like to take a username and say 'Hello, Username' we can take a string object and modify the string object to display 'Hello, John Doe' with some simple string formatting.
Python String Formatting The Old Way Syntax
'String Object %s' % 'Value'
'String Object %s' - This our string object that will be changed. %s represents the placeholder for a string object value.
% - This is our operator.
'Value' - The value is content that will replace the placeholder in a string object. If we are replacing one placeholder it can be in a string format if we are replacing more than one placeholder the value must be contained in a tuple or dictionary.
Examples Of Python String Formatting The Old Way
#Example 1
'This is a %s' % 'String'
'This is a String'
#Example 2
a = 'Hello, %s'
userName = 'Johnny'
a % userName
'Hello, Johnny'
#Example 3
'We are learning %s %s' % ('Python', '3')
'We are learning Python 3'
#Example 4
'We are learning %(language)s %(version)s' % {'language' : 'Python', 'version' : '3'}
'We are learning Python 3'
Examples Explained
Example 1:
'This is a %s' % 'String' - In this example we create a string object with a placeholder that will accept a string. Then we place a value on the right side of the operator. The value will replace the placeholder. Since we only had one value to insert we can keep our value in a string format. We could still use the tuple or dictionary.
'This is a String' - We are returned a new string object that contains our value which replaced the placeholder.
Example 2:
a = 'Hello, %s' - We create a new string object that contains a placeholder for a string. The we give that string object a variable of 'a'.
userName = 'Johnny' - We create a new string object that holds our users name. We then assign the variable named 'userName' to the string object.
a % userName - We call our string object via the variable 'a' and then on the right side of operator we add a value via the variable 'userName'.
'Hello, Johnny' - We are returned a new string object that contains our first string object with the placeholder replaced by our value which was represented by 'userName'.
Example 3:
'We are learning %s %s' % ('Python', '3') - We create a new string object that contains two place holders for strings. Then on the right side of the operator we create a tuple to hold our two values that we will insert into our new string object. Remember that a tuple or dictionary must be used if there is more than one value to be inserted.
'We are learning Python 3' - We are returned a new string object that contains our new values
Example 4:
'We are learning %(language)s %(version)s' % {'language' : 'Python', 'version' : '3'} - We set up a string object that contains two place holders. These two place holders contain the keys from the dictionary. The keys are the values on the left hand side of the colon in a dictionary. The keys must be contained in parentheses and we still provide a type value on the outside of the parentheses. When the keys are called they will insert their values in place of the key.
'We are learning Python 3' - We are returned a new string object that contains the values in our dictionary which were called via their keys.
Conclusion
In this Python tutorial, we discussed Python string formatting the old way. If you have any questions about formatting Python strings leave a comment below and we will help you.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment