How to embed a variable in a python string

How to embed a variable in a python string

・ Here, I will introduce two.

・ 1, f-string ・ 2, format ()

f-string A character string defined by prefixing with f

a="apple"
f"I like an {a}"#Replace with variable value

Execution result


I like an apple

format() {} In the string is replaced with the value passed to the argument of method str.format ().

a="apple"
"I like an {}".format(a)

Execution result


I like an apple

Replace multiple arguments

・ 1.f-string

a="apple"
b="orange"
f"I like an {a} and an {b}"

Execution result


I like an apple and an orange

2.format()

a="apple"
b="orange"
"I like an {} and {}".format(a,b)

Execution result


I like an apple and an orange

Recommended Posts

How to embed a variable in a python string
I want to embed a variable in a Python string
How to convert / restore a string with [] in python
[Python] How to expand variables in a character string
[Python] How to invert a character string
How to get a stacktrace in python
How to make a string into an array or an array into a string in Python
[Introduction to Python] How to output a character string in a Print statement
How to check the memory size of a variable in Python
How to get a string from a command line argument in python
How to clear tuples in a list (Python)
How to create a JSON file in Python
How to notify a Discord channel in Python
[Python] How to draw a histogram in Matplotlib
How to develop in Python
How to write a string when there are multiple lines in python
Parse a JSON string written to a file in Python
How to get the variable name itself in python
How to write string concatenation in multiple lines in Python
How to execute a command using subprocess in Python
[Linux] How to put your IP in a variable
How to write a Python class
[Python] How to do PCA in Python
How to collect images in Python
How to use SQLite in Python
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
6 ways to string objects in Python
How to use PubChem in Python
Create a random string in Python
How to handle Japanese in Python
How to slice a block multiple array from a multiple array in Python
A story about how to specify a relative path in python.
How to use the __call__ method in a Python class
How to import a file anywhere you like in Python
How to define multiple variables in a python for statement
I tried "How to get a method decorated in Python"
How to develop in a virtual environment of Python [Memo]
How to get the last (last) value in a list in Python
How to get a list of built-in exceptions in python
Python learning basics ~ How to output (display) a character string? ~
A story about trying to implement a private variable in Python.
[Introduction to Python] How to use class in Python?
How to access environment variables in Python
How to dynamically define variables in Python
[Python] How to make a class iterable
How to do R chartr () in Python
[Python] How to convert a 2D list to a 1D list
[Itertools.permutations] How to put permutations in Python
How to work with BigQuery in Python
How to display multiplication table in python
How to extract polygon area in Python
How to check opencv version in python
How to switch python versions in cloud9
How to adjust image contrast in Python
How to use __slots__ in Python class
How to dynamically zero pad in Python
Generate a class from a string in Python
How to use regular expressions in Python
How to display Hello world in python