Linked Questions
194 questions linked to/from String formatting: % vs. .format vs. f-string literal
98 votes
4 answers
90k views
Python way of printing: with 'format' or percent form? [duplicate]
In Python there seem to be two different ways of generating formatted output: user = "Alex" number = 38746 print("%s asked %d questions on stackoverflow.com" % (user, number)) print("{0} asked {1} ...
3 votes
2 answers
3k views
python usage of percentage sign- doesn't appear to be modulo or string formatting [duplicate]
What does a % sign mean in python when it is not a modulo or a string formatter? I came across it in this baffling block of code in the timeit module: # Don't change the indentation of the template; ...
2 votes
3 answers
9k views
What's the difference between f"{x}" and "{}".format(x) in python? [duplicate]
I'm new to python, and I just want to know the difference between those examples. Does it differ when it comes to the execution speed? When do I use one of them instead of the other? x: int = 64 print(...
1 vote
3 answers
15k views
Adding line numbers to a file [duplicate]
I need to write the line numbers to an already existing text file in python 3. They have asked for the first 5 columns of the text file to contain a 4 digit line number followed by a space. I've ...
2 votes
2 answers
830 views
String formatting: better to use '%' or 'format'? [duplicate]
I use python 3.4 and I can format strings in two ways: print("%d %d" %(1, 2)) and print("{:d} {:d}".format(1, 2)) In documentation they show examples only using 'format'. Do it mean that using '%' ...
-1 votes
1 answer
7k views
How do i use the % placeholder to write something in python? [duplicate]
I have been asked to use the % placeholder to write something in python. I don't know how to do it can you help.
2 votes
1 answer
6k views
Difference between r and rf in python [duplicate]
I have the following code def latex_template(name, title): return '\n'.join((r"\begin{figure}[ht]", r" This is a custom LaTeX template!", r&...
2 votes
1 answer
3k views
Convert f-string syntax to print in older versions of python [duplicate]
How do i convert an f-string in python so that it can be printed by python version older than 3.6? print(f"Player 1: {move1} Player 2: {move2}") When I try to run this in Python (3.5.3) it gives me ...
0 votes
2 answers
4k views
String injection of Python3 command-line args [duplicate]
New to Python, I have the following script: #!/usr/bin/env python3 import sys env = sys.argv[1:] # The name of the environment we're executing over ('Dev', 'Test', 'Prod', etc) print('Executing ...
-1 votes
3 answers
3k views
Grok Learning: Halve this TypeError [duplicate]
I have to write a program that reads in a whole number and prints out that number divided by two. This is my code: a= int(input("Number: ")) h= a/2 print("Half number: " + h) But I keep getting ...
-1 votes
1 answer
1k views
Please explain '%' usage in python [duplicate]
I'm learning multithreading in python and I was reading through this answer. I understand most of the code however there is this one line which I simply don't understand and I don't know how to search ...
0 votes
1 answer
1k views
How do I pass a Python variable to Bash Shell over SSH connection? [duplicate]
I try to check file existence on a remote server. Everything works fine for me, except that I want to pass the file path as a variable. Here is what I do _,stdout,_=ssh.exec_command("[ -f d:/tryssh/...
-1 votes
2 answers
665 views
How can I export my quiz results to a csv file using python 3? [duplicate]
I want the results to be in the format Date, Question, Name and Score file = open("results.csv", "a") file.write('Date, Question, Name, Score\n' + date + ',' question + ',' + name + ',' + score + '\...
-1 votes
1 answer
680 views
what does {} and format do in this command python? [duplicate]
Hi i am reading a source and it has a line: fn = 'total-listmix-{}-{}.xml'.format(opt.os, opt.compiler) exept Exception as e: raise Exception('error merging coverage: {}'.format(e)) I think the {}...
-3 votes
1 answer
334 views
How to include variables in a string? [duplicate]
I'm doing some coursework and I need to determine a character's name. This is what I have so far: charOne=input("Please input your first character's name: ") charTwo=input("Please input your second ...