Learning Python Programming Language
In the previous video, we discussed the print function and how to print something on the console. We
also did a coding exercise related to the print function. Now, we will learn how to concatenate strings in
Python.
Printing with One Print Function
Instead of using three print functions to print three lines, we can use one print function with the slash n
symbol to add a new line:
Print(“hello world\nhello world\nhello world”)
We can also print “hello world” three times in different lines:
Print(“hello world\nhello world\nhello world”)
Concatenating Strings
To concatenate two strings, we can use the plus symbol:
Print(“hello” + “jenny”)
If we want to add a space between the two strings, we can use any of the following methods:
Add a space after “hello”: print(“hello “ + “jenny”)
Add a space before “jenny”: print(“hello” + “ jenny”)
Add a space in between: print(“hello” + “ “ + “jenny”)
However, we need to be careful when using spaces in Python. Adding a space or tab before writing the
code can cause an indentation error.
Indentation Error
If we add a space or tab before writing the code, we will get an indentation error:
IndentationError: unexpected indent
Print(“hello”)
In the previous video, we discussed the print function and how to print something on the console. We
also did a coding exercise related to the print function. Now, we will learn how to concatenate strings in
Python.
Printing with One Print Function
Instead of using three print functions to print three lines, we can use one print function with the slash n
symbol to add a new line:
Print(“hello world\nhello world\nhello world”)
We can also print “hello world” three times in different lines:
Print(“hello world\nhello world\nhello world”)
Concatenating Strings
To concatenate two strings, we can use the plus symbol:
Print(“hello” + “jenny”)
If we want to add a space between the two strings, we can use any of the following methods:
Add a space after “hello”: print(“hello “ + “jenny”)
Add a space before “jenny”: print(“hello” + “ jenny”)
Add a space in between: print(“hello” + “ “ + “jenny”)
However, we need to be careful when using spaces in Python. Adding a space or tab before writing the
code can cause an indentation error.
Indentation Error
If we add a space or tab before writing the code, we will get an indentation error:
IndentationError: unexpected indent
Print(“hello”)