Using f-strings:

plot.savefig(f'hanning{num}.pdf')

This was added in 3.6 and is the new preferred way.


Using str.format():

plot.savefig('hanning{0}.pdf'.format(num))

String concatenation:

plot.savefig('hanning' + str(num) + '.pdf')

Conversion Specifier:

plot.savefig('hanning%s.pdf' % num)

Using local variable names (neat trick):

plot.savefig('hanning%(num)s.pdf' % locals())

Using string.Template:

plot.savefig(string.Template('hanning${num}.pdf').substitute(locals()))

See also:

  • Fancier Output Formatting - The Python Tutorial
  • Python 3's f-Strings: An Improved String Formatting Syntax (Guide) - RealPython
Answer from Dan McDougall on Stack Overflow
🌐
IncludeHelp
includehelp.com › python › passing-string-value-to-the-function.aspx
Python | Passing string value to the function
Here, we will learn how to pass string value to the function? Consider the below example without sample input and output: Input: str = "Hello world" Function call: printMsg(str) Output: "Hello world" # Python program to pass a string to the function # function definition: it will accept # a string parameter and print it def printMsg(str): # printing the parameter print(str) # Main code # function calls printMsg("Hello world!") printMsg("Hi!
Discussions

ssh - How to pass string from bash to python function - Unix & Linux Stack Exchange
I have a python function which takes a parameter as input and I want to automate and call it through bash. But bash is giving error when i pass variable(string) when calling it More on unix.stackexchange.com
🌐 unix.stackexchange.com
how to pass variable values to a string in python - Stack Overflow
I have requirement where I am reading a string from json file and assign values to it to make it as a configurable file name. String I am reading from json: data_file_format = " More on stackoverflow.com
🌐 stackoverflow.com
How to pass string as variable in Python? - Stack Overflow
I used Context Manager: cd from here: How do I "cd" in Python? import os class cd: """Context manager for changing the current working directory""" def __init__(self, newPath): ... More on stackoverflow.com
🌐 stackoverflow.com
How to insert a variable (string) in middle of URL
are you missing a separator between the domain name and the next bit of the query string? import requests domain = input() domainoutput = requests.get("https://api.threatminer.org/v2/domain.php?q="+domain+"&rt=1") print(domainoutput.text) Note. An f-string would be easier to read: requests.get(f"https://api.threatminer.org/v2/domain.php?q={domain}&rt=1") More on reddit.com
🌐 r/learnpython
4
3
December 20, 2019
🌐
Built In
builtin.com › articles › python-variable-in-string
How to Insert a Python Variable in a String | Built In
F-string looks nice, but it requires Python version 3.6 or greater to do it, making it less accessible. The % format isn’t a bad option. The comma method should only be used if you need a quick solution. A string variable in Python is a variable with a sequence of characters, or a string, assigned to it.
🌐
GeeksforGeeks
geeksforgeeks.org › python › insert-a-variable-into-a-string-python
Insert a Variable into a String - Python - GeeksforGeeks
July 23, 2025 - Explanation: Using the + operator, the variables a and b are joined with a space between them, manually concatenating strings to create dynamic content. This method was used in earlier versions of Python and is still supported, but it's less ...
🌐
CodeGenes
codegenes.net › blog › how-to-pass-variable-in-string-python
Mastering Variable Passing in Strings in Python — codegenes.net
This blog will explore the different ... The basic idea behind passing variables into strings is to replace placeholders in a string with the actual values of variables....
🌐
Data Science for Everyone
matthew-brett.github.io › teaching › string_formatting.html
Inserting values into strings — Tutorials on imaging, computing and mathematics
If you can depend on having Python >= version 3.6, then you have another attractive option, which is to use the new formatted string literal (f-string) syntax to insert variable values. An f at the beginning of the string tells Python to allow any currently valid variable names as variable ...
Find elsewhere
🌐
HackerEarth
hackerearth.com › practice › python › getting started › string
String Tutorials & Notes | Python | HackerEarth
In this tutorial you will see how ... your code. To create a string, put the sequence of characters inside either single quotes, double quotes, or triple quotes and then assign it to a variable....
🌐
Python Guides
pythonguides.com › create-a-string-with-variables-in-python
Ways to Insert a Python Variable into a String
February 2, 2026 - Since Python 3.6, I’ve almost exclusively used f-strings. They are fast, readable, and very easy to type. You just need to add the letter f before your string and put your variables inside curly braces {}.
🌐
Python Examples
pythonexamples.org › python-variables-in-string
How to place Variables in String in Python?
To place variables in string, we may use formatted string literals. Place the character f before you start a string literal. Enclose the variables with curly braces {variable} and place this variable inside the string value, wherever required.
🌐
A Girl Among Geeks
agirlamonggeeks.com › home › how do you pass a variable inside a string in python?
How Do You Pass a Variable Inside a String in Python?
July 4, 2025 - Example: “`python user = “Carol” balance = 123.45 message = “User %s has a balance of $%.2f” % (user, balance) “` This results in: “User Carol has a balance of $123.45” ... Although still functional, this method is generally discouraged in favor of `format()` or f-strings due to less flexibility and readability. The following table summarizes the key characteristics of the three primary methods to pass variables into strings:
🌐
W3Schools
w3schools.com › python › gloss_python_assign_string_variable.asp
Python Assign String Variables
Python Examples Python Compiler ... Python Training ... Assigning a string to a variable is done with the variable name followed by an equal sign and the string:...
🌐
Sololearn
sololearn.com › en › Discuss › 3275984 › how-to-use-variables-in-a-python-string
How to use 'variables' in a 'python' string
May 5, 2024 - Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.
🌐
Quora
quora.com › How-do-I-insert-a-variable-into-a-string-in-Python
How to insert a variable into a string in Python - Quora
Answer (1 of 4): Method #1: using String concatenation In the first method, we'll concentrate a variable with a string by using +. Let's see the following example. [code] # variable variable = "Python" # insert a variable into string using String ...
🌐
Reddit
reddit.com › r/learnpython › how to insert a variable (string) in middle of url
r/learnpython on Reddit: How to insert a variable (string) in middle of URL
December 20, 2019 -

Getting my feet wet with python. Did automate the boring stuff and am now exploring how to use API's with Python.

What I am trying to do, is request passive DNS data from Threatminer based on domainname. The API documentation (https://www.threatminer.org/api.php) states that you can use the rt=2 flag for this. Example they give with the vwrm.com domain: https://api.threatminer.org/v2/domain.php?q=vwrm.com&rt=2

So I thought this would be easy. I thought just creating a variable and feeding it to the URL would be sufficient:

   domain = input()     domainoutput = requests.get(         "https://api.threatminer.org/v2/domain.php?q="+domain+"rt=1")     print(domainoutput.text) 

However, I keep getting the response:

{"status_code":"404","status_message":"No results found.","results":[]} 

To me, this signals that the request was sent, but that no results were found. However, I have the sneaky suspicion that the actual domain that is entered in input is not correctly fed into the url. I am tripping up somewhere, but no idea where. Can someone tell me how to fix this/where to read up on how to fix this?

🌐
PyTutorial
pytutorial.com › python-variable-in-string
PyTutorial | Python: Add Variable to String & Print Using 4 Methods
September 5, 2023 - # Variables name = "Pytutorial" age = 22 # Insert Variables into String greeting = "Hello, %s! You are %s years old" % (name, age) # Print Result print(greeting) ... The str.format() method in Python is used for string formatting and is an ...