how to use fstring with .join() method in python

You can use f-string and join methods in combination in Python as shown below:

main.py
my_list = ["apple", "banana", "cherry"]
result_string = f"List of fruits: {' / '.join(my_list)}"
print(result_string)
118 chars
4 lines

In the above example, we have created a list of fruits named my_list. We have then used the .join() method to join the contents of the list with a separator of /. Then, we have used f-string to include this result string in another string.

Output:

main.py
List of fruits: apple / banana / cherry
40 chars
2 lines

Here, the output consists of the list of fruits separated by /.

related categories

gistlibby LogSnag