dashwood.net -

Ryan Stefan's Micro Blog

Stand Alone Scrapy in Action + New Dev Tricks

Jan 172019

Python 3 New String Formatting

I've been using the .format method and explicitly calling variables in case I change the order down the line for all of my code because it just seemed the cleanest way:

print('this is a string {variable}'.format(variable=variable))

But there is a much simpler/cleaner method called f-strings. They are shorter, easier to read, and just plain more efficient. I'll be refactoring any code I come across using the older formatting styles just our of principle. Using this format with the scraping project I worked on this week was a significant convienence and I looooove it. Here's how it works (so simple):

test = 'this'

number = 254

print(f"{test} is a test for number {number}.")
this is a test for number 254.

Comments