Python String Concatenation: Combine Text and Variables
Learn how to combine variables and text in print messages.
Now that Python can remember things, let’s make our messages more dynamic!
This lesson will teach us how to combine words and numbers to create full sentences using variables.
Mix it up
Try this code:
Awesome! We just combined strings and numbers in one line.
Why commas?
In print(), commas separate each part of our message. Python puts them together and even adds spaces for us.
We can mix:
Strings (text in quotes)
Variables (like
name,age)Numbers
All in a single print() statement.
Your turn: Build a fun message
Now swap in your own favorite things!
We just made a custom sentence using commas.
How do you reverse a string in Python?
In Python, strings are ordered sequences of character data.There is no built-in method to reverse a string. However, strings can be reversed in several different ways.
Methods
Three methods to reverse a string are explained below:
1. Slicing
Strings can be reversed using slicing. To reverse a string, we simply create a slice that starts with the length of the string, and ends at index 0.
To reverse a string using slicing, write:
stringname[stringlength::-1] # method 1
Or write without specifying the length of the string:
stringname[::-1] # method2
The slice statement means start at string length, end at position 0, move with the step -1 (or one step backward).
2. Loop
To start, let’s create a new array called reversedString[].
We can then loop over the string with iterating variable index initialized with the length of the string.
In each iteration, concatenate value of str[index-1] with reverseString
Decrement the index.
We then simply keep iterating until the index is less than zero.
3. Use join
This is a powerful technique that takes advantage of Python’s iterator protocol. This technique reverses a string using reverse iteration with the reversed() built-in function to cycle through the elements in the string in reverse order and then use .join() method to merge all of the characters resulting from the reversed iteration into a new string.
The general syntax is
s="Python"reversedstring=''.join(reversed(s))
The following Python code demonstrates the concept.
What is the __str__ method in Python?
The __str__ method in Python represents the class objects as a string – it can be used for classes. The __str__ method should be defined in a way that is easy to read and outputs all the members of the class. This method is also used as a debugging tool when the members of a class need to be checked.The __str__ method is called when the following functions are invoked on the object and return a string:The print() methodThe str() methodIf we have not defined the __str__, then it will call the __repr__ method. The __repr__ method returns a string that describes the pointer of the object by default (if the programmer does not define it).How to call __str__ methodLet’s explore different ways to call the __str__ method.
1. Default implementation
The following code explains the default implementation of the __str__ method.
The above code shows an example where neither __str__ nor __repr__ are defined. Calling __str__ calls the default __repr__ method, and they all give the same output, the pointer of our object.
2. Custom __str__ method
The following code explains how the custom __str__ method works.
The code above shows the output once you have defined the __str__ method. When __str__, print(), or str() are called you will get your defined output. Make note that the __repr__ output remains the same.
3. The __repr__ method defined only
Let’s see the example of the __repr__ method defined only.
In the first example we saw that when __str__ is not defined it automatically calls the __repr__ method. Therefore, the output of all the functions - __str__, str(), and __repr__ - are the same. Moreover, the __repr__ method does not necessarily need to return a string. In case it does not return a string, the print() statements will throw an error.