How to format string with values from list?

Ethr

How to print next values of list in place of %s? Code below shows result which I expect to get, but I don't want to write every step manually.

hours=["14","15","18","30"]
print (("%s:%s-%s:%s")%(hours[0],hours[1],hours[2],hours[3]))

Is there method to do something like this:

print (("%s:%s-%s:%s")%hours)

and make it work?

Psidom

You can use format, and use * to unpack values in the list as parameters to the format method:

"{}:{}-{}:{}".format(*hours)
# '14:15-18:30'

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to extract and format specific values from a string

How to do a IN from a list in string format

Format data of string of dates and associated values from an unstructured list

How to get float values from string list

How to remove repeated string values from a list

How to create a String with String.format by taking parameters from a list

How can I get values from list of dictionary in a format

How to get repeated tag values from an xml in string format (Java)

How to map values from List<String> into a List<Model>

How to drop string from a List that contain string values and dataframes?

Generate format string based on values in list

How to string format a whole list?

String creation from list of values

How to make a String from two set of list with alternate values

How to get unique values with frequency from List<String[]> in Java?

How to create a column in a dataframe using filtering for string values from a list?

How do I input values from a list into a string?

How to build a dataframe from values in a json string that is a list of dictionaries?

how to get string values from list of maps in dataweave 2.0?

How can I build a string from a value and a list of values?

How to extract values from a column of list string pandas dataframe

How do I remove string '' from python dictionary list values?

How to create a list of string from columns values in a dataframe

How to change the values of dictionary from string to int if they are in a list

C# How to store string values in a list outputted from a loop

Get values from a string based on a format

How to return values from a parameterized List<string> (int number, List<string> lines)

how to remove key value pairs from a dictionary based on values which are in list format and having some intersection with other list values

Regex format to find a specific string from the list