names = ['David', 'Peter', 'Michael', 'John', 'Bob']
for i in range (len (names)):
    print("{}.{}".format(i + 1, names[i]))

Python list index references cannot be strings. Iterating through the list via a for loop using integers rather than the indexes themselves (which are strings) will solve this issue. This is an example where the error message is very useful in diagnosing the problem.

Answer from Reece on Stack Overflow
🌐
Python for Network Engineers
pyneng.readthedocs.io › en › latest › book › 08_python_basic_examples › f-string.html
Formatting lines with f-strings - Python for network engineers
When using f-strings in loops an f-string must be written in body of the loop to «catch» new variable values within each iteration: In [1]: ip_list = ['10.1.1.1/24', '10.2.2.2/24', '10.3.3.3/24'] In [2]: for ip_address in ip_list: ...: ip, mask = ip_address.split('/') ...: print(f"IP: {ip}, ...
Discussions

Python string formatting: For loops? - Stack Overflow
I really enjoy the Django/Jinja style of formatting. I understand wanting to keep logic and presentation separate, but the if statements and for loops are ideal when reporting a changing amount of ... More on stackoverflow.com
🌐 stackoverflow.com
May 24, 2017
String formatting in a loop
Try: '\n'.join(result) More on reddit.com
🌐 r/learnpython
15
4
April 20, 2015
Python: Loop: formatting string - Stack Overflow
With "_%s_" as a substring of that. How to get the main string when I format the substring? (as a shortcut of: ... I think something like this is exactly what he's looking for... Just adding a string onto another in every loop iteration. More on stackoverflow.com
🌐 stackoverflow.com
July 20, 2016
list - How to loop with string formatting in Python? - Stack Overflow
I have two variables that I am constructing a new string from, however one of the variables isn't looping correctly. I am expecting several values, but the resultant output is the first values fr... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Esri Community
community.esri.com › t5 › python-questions › for-loop-and-python-string-format › td-p › 608446
Solved: For Loop and Python String format() - Esri Community
December 12, 2021 - Hello, I am trying to use UpdateCursor to update a field value by evaluating the value of another field. I have to do this in about 70 feature classes. For this reason I am using For Loop as well. This is the script: import arcpy arcpy.env.workspace = r"D:\APRX_MXDS\USA_Parcels_2019_Project\Test.g...
🌐
Annedawson
annedawson.net › Python3_Repetition_StringFormatting.htm
Python 3 - Repetition and String Formatting by Anne Dawson, PhD
March 1, 2021 - PLEASE REFRESH THIS PAGE TO GET THE LATEST VERSION · Like the if statement, the while statement makes use of a Boolean expression
Find elsewhere
🌐
Python
docs.python.org › 3 › library › string.html
Common string operations — Python 3.14.6 documentation
In addition, the Formatter defines a number of methods that are intended to be replaced by subclasses: ... Loop over the format_string and return an iterable of tuples (literal_text, field_name, format_spec, conversion).
🌐
Reddit
reddit.com › r/learnpython › string formatting in a loop
r/learnpython on Reddit: String formatting in a loop
April 20, 2015 -

Hello, I have another probably very trivial question

I was doing something unrelated and ended up with code similar to this:

...
for result in db:
    logging.info('RESULTS: {}'.format(result))

What I want here would be

RESULTS:
    a
    b
    c
    ...

But, of course, this doesn't work

My first instinct was something like this:

...
log = 'RESULTS:'
for result in db:
    log = 'log' + result \n
logging.info(log)

Which works, but I read about and string concatenation isn't efficient at all. In this particular case the database is really tiny, so it's fine. But I was wondering, how could I achieve the same for a relatively large (not really huge) database? Is there a way to concatenate using .format?

🌐
W3Schools
w3schools.com › python › python_for_loops.asp
Python For Loops
Remove List Duplicates Reverse ... Study Plan Python Interview Q&A Python Bootcamp Python Training ... A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a stri...
🌐
Stack Overflow
stackoverflow.com › questions › 30836097 › how-to-loop-with-string-formatting-in-python
list - How to loop with string formatting in Python? - Stack Overflow
String formatting. Edit: List comprehension [x for x in range(5)]. Dictionary comprehension: {x: my_items[x] for x in range(5)}.
🌐
Real Python
realpython.com › python-f-strings
Python's F-String for String Interpolation and Formatting – Real Python
November 30, 2024 - The timeit.timeit() function inside the for loop runs each interpolation tool a million times and returns the total execution time. Then, the function prints the result to the screen. Note how your f-string in the call to print() takes advantage of format specifiers to conveniently format the code’s output.
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-use-string-formatters-in-python-3
How To Use String Formatters in Python 3 | DigitalOcean
August 20, 2021 - Formatters can be seen in their best light when they are being used to organize a lot of data in a visual way. If we are showing databases to users, using formatters to increase field size and modify alignment can make your output more readable. Let’s look at a typical for loop in Python that will print out i, i*i, and i*i*i in the range from 3 to 12:
🌐
Reddit
reddit.com › r/learnpython › how can i string format a single variable (that repeats) at multiple locations in a string?
r/learnpython on Reddit: How can I string format a single variable (that repeats) at multiple locations in a string?
August 25, 2022 -

I am currently learning the string formatting topic and I have searched many places only to get *args and *kwargs (completely unrelated to my question) as my answer. Coming back to my question....

Let's say I have a code to convert decimal numbers to binary, octal and hexadecimal. I wrote the code something like this

num = int(input("Enter an integer: "))

print("Binary of {0} is {1:b}".format(num, num))
print("Hexadecimal of {0} is {1:x}".format(num, num))
print("Octal of {0} is {1:o}".format(num, num))

Here, in the string format method, I am passing 'num' twice to reflect in the printed message. Is there anyway to pass 'num' once to all the placeholders {} in the string?

One more example:

can you can a can as a canner can can a can

For a statement like this, you can code something like below which has repeating "can"s. Instead I want to pass a single string variable for all the placeholders

print("{0} you {1} a {2} as a canner {3} {4} a {5}".format("can", "can", "can", "can", "can", "can"))

Note: Please do suggest a solution to the above, if one such exists. If it doesn't, you can say no. Please please please avoid statements like "You can directly print the string as such instead of going through this ordeal!"