Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Insert a number in string - Python
Next article icon

The goal here is to insert a variable into astring in Python.For example, if we have a variable containing the word "Hello" and another containing "World", we want to combine them into a single string like "Hello World". Let's explore the different methods to insert variables into strings effectively.

Using f-strings

f-strings(formatted string literals) were introduced in Python 3.6 and have quickly become the most recommended way to insert variables into strings. They are concise, readable and faster than other traditional methods.

Python
a="Python"res=f"This is{a} programming!"print(res)

Output
This is Python programming!

Explanation: In an f-string, the variable a is inserted directly by placing it inside {} within a string prefixed withf, enabling dynamic content without manual concatenation.

Using format()

Before f-strings, the.format() method was the standard way to insert variables into strings. It remains widely used, especially in projects requiring compatibility with Python versions earlier than 3.6.

Python
a="Hello"b="world"res="{}{}".format(a,b)print(res)

Output
Hello world

Explanation: In the format() method, placeholders {} are used inside the string, and variablesaand bare inserted by passing them to format(), allowing dynamic content without manual concatenation.

Using + operator

+ operator is a basic method to combine strings and variables, but it can get messy with multiple variables or different data types.

Python
a="Hello"b="World"res=a+" "+bprint(res)

Output
Hello World

Explanation: Using the + operator, the variables a and b are joined with a space between them, manually concatenating strings to create dynamic content.

Using % formatting

This method was used in earlier versions of Python and is still supported, but it's less preferred today due to better alternatives likeformat()and f-strings.

Python
a="Python"res="This is%s programming!"%aprint(res)

Output
This is Python programming!

Explanation: In%formatting, the %s placeholder inside the string is replaced by the value ofa, allowing variable insertion in a style similar to old C-style formatting.


Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp