December 2018
Beginner to intermediate
796 pages
19h 54m
English
One of the features strings have is the ability to be used as a template. There are several different ways of formatting a string, and for the full list of possibilities, I encourage you to look up the documentation. Here are some common examples:
>>> greet_old = 'Hello %s!'>>> greet_old % 'Fabrizio''Hello Fabrizio!'>>> greet_positional = 'Hello {} {}!'>>> greet_positional.format('Fabrizio', 'Romano')'Hello Fabrizio Romano!'>>> greet_positional_idx = 'This is {0}! {1} loves {0}!'>>> greet_positional_idx.format('Python', 'Fabrizio')'This is Python! Fabrizio loves Python!'>>> greet_positional_idx.format('Coffee', 'Fab')'This is Coffee! Fab loves Coffee!'>>> keyword = 'Hello, my name is {name} {last_name}'>>> keyword.format(name='Fabrizio', ...Read now
Unlock full access