How to Use tostring() Equivalent in Python

In any programming language, the string data type is frequently used to input a sequence of characters. The string is the arrangement of characters in a series. As a programmer, you would have gone through the “tostring()” method in various programming languages. The “tostring()” converts various data types into strings. In Python, we do not have the “tostring()” method available. However, the “str()”, “format()” and “f-string” functions are “tostring()” equivalent methods in Python.

This post will explore the below listed methods used as “tostring()” equivalent in Python:

Let’s start with the “str()” function first:

Method 1: Using str() Function

The built-in “str()” function is used to convert any variable to a string in Python. When we call the str() function in Python, it will convert the value into a string and save the value into a new variable. The original variable will not change.

The syntax of “str()” function is shown below:

Syntax

str(object, encoding=encoding, errors=errors)

The parameter “object” returns the value in string format. The “encoding” and “errors” parameter is optional, it is not necessary to define them inside the parentheses.

Check out the example for “str()” function below:

Code

#using str() function
val_1 = 3.5
val_2 = str(val_1)

print(val_1, type(val_1))
print(val_2, type(val_2))

In the above code:

  • The variable named “val_1” is initialized with a float value.
  • The “str()” function is applied on the “val_1” variable, and the value will be stored in the “val_2”.
  • The “print()” statement is used with the “type()” function to print the types of variables “val_1” and “val_2”.

Output

The output shows that the float has converted into the string value.

Method 2: Using format() Function

The “format()” function is another built-in function of Python used to convert objects into the string value. The blank curly braces “{}” are placed inside the double quotation marks along with the “.format()” function. To convert the variable value into a string, pass the variable inside the “.format” function, i.e., “.format(a)”.

The syntax of the “format()” function is shown below:

Syntax

string.format(value1, value2...)

An example for this function is shown below:

Code

#using format() function
value_1 = 3
value_2 = [1, 2, 3]
value_3 = { "Red": 1, "Black": 2}

print("{}".format(value_1), type("{}".format(value_1)))
print("{}".format(value_2), type("{}".format(value_2)))
print("{}".format(value_3), type("{}".format(value_3))) 

In the above code:

  • Three variables named “value_1” as an integer, “value_2” as a list and “value_3” as a dictionary are initialized.
  • The “.format()” function is used inside the “print()” statement along with the “type()” function to print the type of converted variables.

Output

The output clearly shows that all the different data type values are converted into a string value.

Method 3: Using f-string Method

The “f-string” method is a new feature introduced in Python 3; it will help to convert the different data types into the string value. In the “f-string” method, the character “f” is used before the start of a single quotation. The variable value will be added inside the string using “{}” curly brackets, i.e., for example, f ‘{a}’.

The syntax of “f-string” method is defined as:

Syntax

Variable = f'string value {}'

For further understanding, take a look at the example given below:

Code

#using f string method
position_1 = 12
position_2 = 24

output = f'First position is {position_1} and the second position is {position_2}.'

print(output)

In the above code:

  • The two variables named “position_1” and “position_2” are initialized.
  • The “f-string” method is used to add the variable value {position_1} and {position_2} inside the curly bracket into the string.
  • The final output is printed into a single string value and saved in a variable named “output”.

Note: In the “f-string” method, the variables in curly brackets are automatically converted to strings.

Output

In the above output, the variable value “12” and “14” is converted and added into a single string.

That’s all from this guide!

Conclusion

In Python, built-in functions like “str()”, “format()”, and the “f-string” method are used as an equivalent of the “tostring()” function. With the help of these functions, we convert any object or variable value into a string. The “str()” function takes a variable value as a parameter and converts it into a string value. The “format()” function converts the value into a string by taking a variable in the argument. The “f-string methods” add any variable value inside the string and convert the variable type into a string.

This tutorial post presented a complete guide on using the “tostring()” equivalent in Python using some suitable examples.

Over 500 Million Views on Our Blogs and YouTube! We’re inviting 10 businesses to partner with us and grow together. Click here to join us! ×