Python Friday #105: Multiply Strings in Python

While working through a Pluralsight course I noticed a strange looking command that seams to make no sense: ‘-‘ * 100. As it turns out, we can multiply strings in Python and this provides some nice amenities for our cli applications.

This post is part of my journey to learn Python. You can find the other parts of this series here. You find the code for this post in my PythonFriday repository on GitHub.

 

Multiplying strings?

Multiplying numbers is something basic that all programming languages offer, and Python is no exception:

However, in Python we can replace one part of the multiplication with a string and it still works:

If we want to create a line of – to split parts of the output in a Python script, we can write this call to print():

 

Conclusion

I find string multiplications a surprisingly usable feature and it will reduce a lot of typing when I need to print a separator for my cli tools.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.