Python Tutorial– Concatenate Function

Python Exercise Lists

Concatenating (joining) strings in Python is very simple comparing to other programming languages. You can write two strings directly next to each other in the following format:

string_id = "str_1" "str_2"

<

string_id represents the string variable name after concatenation, str_1 and str_2 are the contents of the string to be concatenated. Using this notation, Python will automatically join two strings together.

[Example] Concatenate strings in the form of continuous writing:

string_1 = "Welcome to" "Free Learning Points"
print(string_1)
string_2 = "addition" "subtraction" "multiplication" "division"
print(string_2)

The output is:

Welcome toFree Learning Points
additionsubtractionmultiplicationdivision

It should be noted that this writing method can only concatenate string constants.

If you need to concatenate variables, you must use the + operator to join them together, the specific format is:

string_id = string_1 + string_2

Of course, the + operator can also concatenate string constants.

[Example] Use + operator to concatenate strings:

string_1= "Hello,"
string_2 = "Free Learning Points"
string_id = string_1 +  "welcome to" + string_2
print(string_id)


The output is:

Hello, welcome to Free Learning Points

Python string and number concatenation

In many application scenarios, we need to join strings and numbers together, and Python does not allow direct joining of numbers and strings, so we must first convert numbers to strings. Numbers can be converted to strings with the help of the str() and repr() functions, which are used in the format:

str(obj)

repr(obj)

obj represents the object to be converted. It can be numbers, lists, tuples, dictionaries, and other types of data.

Look at the following code:

name = "Alice"
age = 18
data = name + " is " + str(age) + " years old."
print(data)

The output is:

Alice is 18 years old.

The difference between str() and repr() in Python

Although str () and repr () can both convert numbers to strings, there are differences between them:

  • str() is used to convert data into a human-readable string form.
  • repr() is used to convert the data into a string format (Python expression) suitable for reading by the interpreter, suitable for use during development and debugging; if there is no equivalent syntax, a SyntaxError exception occurs.

Consider the following example:

a = "www.freelearningpoints.com"
a_string = str(a)
a_repr = repr(a)
print(type(a_string))
print(a_string)
print(type(a_repr))

The output is:

[class 'str']
www.freelearningpoints.com
[class 'str']
www.freelearningpoints.com

In this example, a is itself a string, but we still converted it using str() and repr(). It can be seen from the running results that str() retains the original appearance of the string, and repr() uses quotation marks to surround the string. This is the Python string expression form.

In addition, when an expression (variable, addition, subtraction, multiplication, division, logical operation, etc.) is entered in the Python interactive programming environment, Python automatically uses the repr () function to process the expression.

                               


More Tutorials:

Python Installation - Linux (Ubuntu)
Python Installation - Mac OS
Integrated Development Environment - IDE
Python - Basic Variables
Python - Sequence Introduction
Python - Output Formatting
Python - Escape Character
Python - Type Conversion
Python - Numbers
Python – Arithmetic Operators
Python – Assignment Operators
Python – Comparison Operators
Python – Logical Operators
Python – Precedence and Associativity Operators
Python – Bytes Type and Usage
Python – Long & Raw Strings
Python – Concatenate Function
Python – Slice Function
Python – Length and Split Function
Python – Join and Count Function
Python – Find Function
Python – Index Function
Python – Alignment Function
Python – Startswith and Endswith Function
Python – String Case Conversion
Python – Remove Specified Character
Python – Encode and Decode Function
Python – dir and help Function
Python – Input Output Introduction
Python – Basic Operation
Python – Open Function
Python – Read Function
Python – Readline Function
Python – Write Function
Python – Close Function
Python – Seek and Tell Function
Python – Pickle Module
Python - File Input Module and Linecache Module
Python - Pathlib Module
Python - Pathlib Module
Python - os.path Module
Python - fnmatch Module
Python - Tuple Introduction
Python - List Introduction
Python - List Add Element
Python - List Delete Element
Python - List Modification Element
Python - List Find Element
Python - Dictionary Introduction
Python - Dictionary Basic Operation
Python - Dictionary Method Guide
Python - Set Collection
Python - Set Basic Operation
Python - Set Basic Method
Python - Frozenset Method
Python - If Condition I
Python - If Condition II
Python - While loop
Python - For loop
Python - Pass Statement
Python - Break Statement
Python - Zip Reverse Function
Python - Function Introduction
Python - Positional Parameters
Python - Key Arguments
Python - None and Return
Python - Variable Scope
Python - Local Function
Python - Closure Method
Python - Lamdba Expression


More Python Exercises:

Python String Exercises
Python List Exercises
Python Library Exercises
Python Sets Exercises
Python Array Exercises
Python Condition Statement Exercises