How do I put a variable’s value inside a string?
plot.savefig('hanning(%d).pdf' % num)
The %
operator, when following a string, allows you to insert values into that string via format codes (the %d
in this case). For more details, see the Python documentation:
printf
-style String Formatting
How to interpolate variables in strings in JavaScript, without concatenation?
You can take advantage of Template Literals and use this syntax:
`String text ${expression}`
Template literals are enclosed by the back-tick (` `) (grave accent) instead of double or single quotes.
This feature has been introduced in ES2015 (ES6).
Example
var a = 5;
var b = 10;
console.log(`Fifteen is ${a + b}.`);
// "Fifteen is 15.
How neat is that?
Bonus:
It also allows for multi-line strings in javascript without escaping, which is great for templates:
return `
<div class="${foo}">
...
</div>
`;
Browser support:
As this syntax is not supported by older browsers (mostly Internet Explorer), you may want to use Babel/Webpack to transpile your code into ES5 to ensure it will run everywhere.
Side note:
Starting from IE8+ you can use basic string formatting inside console.log
:
console.log('%s is %d.', 'Fifteen', 15);
// Fifteen is 15.
How to insert a variable value in a string in python
Use string formatting:
s = "Your first value is {} your second value is {}".format(amount1, amount2)
This will automatically handle the data type conversion, so there is no need for str()
.
Consult the Python docs for detailed information:
https://docs.python.org/3.6/library/string.html#formatstrings
Putting a variable into a string (quote)
You should use a string formatter here, or concatenation. For concatenation you'll have to convert an int
to a string
. You can't concatenate ints and strings together.
This will raise the following error should you try:
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Formatting:
quote = "You are %d years old" % age
quote = "You are {} years old".format(age)
Concatenation (one way)
quote = "You are " + str(age) + " years old"
Edit: As noted by J.F. Sebastian in the comment(s) we can also do the following
In Python 3.6:
f"You are {age} years old"
Earlier versions of Python:
"You are {age} years old".format(**vars())
R variable inside a string
variable2 <- paste0("I am argument:",variable1,"text continue")
Insert variable values in the middle of a string
You can use string.Format
:
string template = "Hi We have these flights for you: {0}. Which one do you want";
string data = "A, B, C, D";
string message = string.Format(template, data);
You should load template
from your resource file and data
is your runtime values.
Be careful if you're translating to multiple languages, though: in some cases, you'll need different tokens (the {0}
) in different languages.
How an I insert variables at a specific place in a string?
You can use ES2015 template literals. But, the variables should be defined before using them.
var name1 = "Phil";var name2 = "Amy";var templateString = `Hello ${name1}, my name is ${name2}`;
console.log(templateString);document.body.innerHTML = templateString;
Related Topics
How to Match a Whole Word With a Regular Expression
Access Nested Dictionary Items Via a List of Keys
Cartesian Product of X and Y Array Points into Single Array of 2D Points
Why Does the Division Get Rounded to an Integer
Why Do People Write #!/Usr/Bin/Env Python on the First Line of a Python Script
Getting Key With Maximum Value in Dictionary
Why Does On_Message Stop Commands from Working
How to Install Pygame on Python Via Pip (Windows 10)
How to Install Packages Offline
Why Is Nothing Drawn in Pygame At All
Download Large File in Python With Requests
Check If a Given Key Already Exists in a Dictionary
How to Post Json Data With Python Requests
Modifying List While Iterating