Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python - Horizontal Concatenation of Multiline Strings
Next article icon

String concatenation in Python allows us to combine two or more strings into one. In this article, we will explore various methods for achieving this. The most simple way to concatenate strings in Python is by using the+ operator.

Using + Operator

Using+ operatorallows us to concatenation or join strings easily.

Python
s1="Hello"s2="World"res=s1+" "+s2print(res)

Output
Hello World

Explanation: s1 + " " + s2combiness1 ands2with a space between them.

Note: This method is less efficient when combining multiple strings repeatedly.

Let's explore other different method for string concatenation in string:

Using join() Method for Concatenation

Use join() function to concatenate strings with a specific separator. It’s especially useful when working with a sequence of strings, like alist ortuple. If no separator is needed then simply usejoin() with an empty string.

Python
a=["Python","is","a","popular","language","for","programming"]# Use join() method to concatenate list elements# into a single string, separated by spacesres=" ".join(a)print(res)

Output
Python is a popular language for programming

Explanation: " ".join() method combines each element in words with a space between them.

Note: join() is optimal for large-scale string concatenation since it minimizes the creation of intermediate strings.

Using format() Method

Theformat()method provided an easy way to combine and format strings. It is very helpful when we want to include multiple variables or values. By using curly braces {} in a string, we can create placeholders thatformat() will fill in with the values we'll provide.

Python
s1="Python"s2="programming"# Use format() method to create a new string that includes both variablesres="{} is a popular language for{}".format(s1,s2)print(res)

Output
Python is a popular language for programming

Explanation:{} placeholders in the string are replaced by the values inside format().

Using f-strings (Formatted Strings)

F-strings make it easy to combine and format strings in Python. By adding anfbefore the string, we can directly include variables in curly braces {}.

Python
s1="Python"s2="programming"# Use an f-string to create a formatted string# that includes both variablesres=f"{s1} is a popular language for{s2}"print(res)

Output
Python is a popular language for programming

Explanation:Thefbefore the string allows us to use expressions inside curly braces {}.

Which method to choose?

  • + Operator:Simple and readable but not ideal for multiple concatenations in loops.
  • join() Method:Highly efficient for concatenating lists of strings.
  • f-strings:Use when variables are involved.
  • format(): Similar to f-strings but compatible with earlier versions.



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