Escape sequence and string concatenation and format method

Escape sequence and string concatenation and format method

Escape sequences, string concatenation, and the format method are all concepts related to working with strings in programming.

Escape sequences are special characters that allow you to include characters in a string that would otherwise be difficult to type or to represent, such as new lines (\n), tabs (\t), and quotation marks (“). By using an escape sequence, you can tell the computer to interpret the special character in a certain way.

String concatenation is the process of combining two or more strings into a single string. This can be done using the + operator, which allows you to join strings together.

The format method is a way to insert values into a string in a more structured way than string concatenation. This method involves using placeholders in the string that are replaced with values from variables or expressions at runtime. For example, you could use the syntax {} in a string to indicate where a value should be inserted, and then pass that value as an argument to the format method.

Here is an example of how you might use all three concepts together:

# Using escape sequences
print("Hello, world!\n")

# String concatenation
first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name
print("My name is " + full_name + ".")

# Using the format method
age = 25
occupation = "developer"
print("I am {} years old and work as a {}.".format(age, occupation))

This would output:

Hello, world!

My name is John Doe.
I am 25 years old and work as a developer.

Apply for Python Certification!

https://www.vskills.in/certification/certified-python-developer

Back to Tutorials

Strings types single quotes and double quotes and triple quotes
Variables naming and types and objects

Get industry recognized certification – Contact us

keyboard_arrow_up
Open chat
Need help?
Hello 👋
Can we help you?