String formatting
What is string formatting?
String formatting is the process of inserting a custom string or a variable in some predefined text. For example, you have calculated a mean and standard deviation and now you want your program to print these out but in a nice sentence rather than just as two numbers all on their own.
- The mean is 32.4 and the standard deviation is 11.2
vs.
- 32.444444 11.264386542
Maybe you also don't need the numbers to a bajillion decimal places either, rounding would be preferable. String formatting allows you to accomplish this.
How do I use string formatting?
So, there are a number of different ways that you can get string formatting in Python - it has evolved a bit over time but (I think) they are backwards compatible. It's worth being aware of all of them so that if you see them in other people's code, you know what's going on. I only use the most recent version and that's what we'll use in the next exercises.
name = "Jaime" age = 42 my_string = "This is a string made by {} who is {} years old.".format(name, age) print(my_string) # Try changing the values for name and age and confirm that my_string updates
In the example above you have
- a tempate (the string beginning "This is a...")
- placeholders (these are the curly braces, { }, within the string)
- the function (.format, that will be applied to the string
- the variables (name and age, that will be inserted into the placeholders)
There is much more that could be written here, for example regarding positional vs. keyword variables, but I encourage you to look at some of the links at the bottom if you want to delve deeper.
Formatting variables
If you want to format the variables before inserting into the string then you can include codes inside the braces - these things, { } - to tell Python what to do. For example, only show the number to 2 decimal places.
list_of_values = [5, 3, 2, 4, 1, 2, 7, 9, 5, 3, 5, 8, 2,1] mean = np.mean(list_of_values) print(mean) print("The average of all the numbers is {}".format(mean)) print("The average of all the numbers is {:.2f}".format(mean))
In the above example the formatting code (or type) is given by .2f which tells Python to return a floating point number truncated after 2 decimal places. You can find other examples of other codes that can be used in several of the links below but the first link is a "cheat sheet".
https://cheatography.com/maschmidt2/cheat-sheets/python-format-string-syntax/
https://pyformat.info/
https://www.w3schools.com/python/ref_string_format.asp
https://realpython.com/python-formatted-output/
Exercises
Add braces and formatting to the strings below to show...
#1) The mouse ID and the age in days mouseID = "DFD01" age = 143 str1 = "Mouse is currently days old." print(str1) #2) The mouse ID and the age in months to 1 decimal place str2 = "Mouse is currently months old." #3) The mode, the median, and the mean (to 3 decimal places) data = [2,5,4,3,7,9,4,5,7,2,3,7,5,6,10,8,7,3,4,1,1,2,1,4,8,3,6,7,23,4,3,4,7,9,4] str3 = "For the above dataset the mode is , the median is , and the mean is ." #4) The increase as a percentage control = 231.55 experimental = 432.19 str4 = "The experimental group increased by , relative to the control." #5) The p-value in scientific notation to 3 decimal places p_value = 1/2300000821 str5 = "Statistical analysis revealed that the p-value was ."