Skip to main content

How to Convert String Variables

How to Convert a String to Other Types

In the process of working with data, it is often necessary to convert one data type to another. In Python, you can convert different data types to a string and vide versa using various conversion functions. This part will describe how to do it.

Convert to String

To convert a non-string object to a string in Python, you can use the str() function. Here are some examples how to cast data:

# convert an integer to a string
num = 42
str_num = str(num)
print(str_num)  # outputs "42"
print(type(str_num))  # outputs "<class 'str'>"

# convert a float to a string
pi = 3.14159
str_pi = str(pi)
print(str_pi)  # outputs "3.14159"
print(type(str_pi))  # outputs "<class 'str'>"

# convert a boolean to a string
flag = True
str_flag = str(flag)
print(str_flag)  # outputs "True"
print(type(str_flag))  # outputs "<class 'str'>"

Note that if you try to convert an object that doesn't have a defined string representation, you may get a TypeError exception.

Unicode to String

In Python, you can convert a Unicode string to a regular string (also known as a byte string) using the encode method.

Here's an example:

unicode_string = "Hello, World! 🌍"
byte_string = unicode_string.encode("utf-8")
print(byte_string) # Output: b'Hello, World! \xf0\x9f\x8c\x8d'

In this example, the encode method is used to convert the unicode_string to a byte string encoded in UTF-8 format. The resulting byte_string variable contains the byte representation of the original string.

Note that the b prefix in the output indicates that the value is a byte string, rather than a regular string. If you want to convert the byte string back to a regular string, you can use the decode method:

new_unicode_string = byte_string.decode("utf-8")
print(new_unicode_string) # Output: Hello, World! 🌍

In this example, the decode method is used to convert the byte string back to a Unicode string encoded in UTF-8 format. The resulting new_unicode_string variable contains the original string.

String to List Conversion

To convert a string to a list of its individual letters in Python, you can use the built-in list() function. Here's an example:

my_string = "hello"
letters_list = list(my_string)
print(letters_list)

This will output:

['h', 'e', 'l', 'l', 'o']

Alternatively, you could use a loop to iterate over the string and append each letter to a new list:

my_string = "hello"
letters_list = []
for letter in my_string:
    letters_list.append(letter)
print(letters_list)

This will also output:

['h', 'e', 'l', 'l', 'o']

String to Boolean Conversion

You can convert a string to a boolean value using the built-in bool() function.

By default, the following strings are considered as True:

  • Any non-empty string
  • The string "True" (case-insensitive)

On the other hand, the following strings are considered as False:

  • An empty string
  • The string "False" (case-insensitive)
  • Any numeric value equal to 0 (i.e., "0" or "0.0")

Here are some examples:

>>> bool("hello")
True
>>> bool("")
False
>>> bool("True")
True
>>> bool("false")
False
>>> bool("0")
False
>>> bool("1")
True

If you have a string that is not one of the above values and you want to treat it as a boolean, you can define your own rules for conversion using an if statement or a conditional expression.

String to Hex

You can convert a string to its hexadecimal representation in Python using the encode() method and the 'hex' encoding. Let's see how to encode with an example:

string = "Hello, world!"
hex_string = string.encode('hex')

print(hex_string) # Output: 48656c6c6f2c20776f726c6421

In Python 3, the hex() method can be used to convert a string to its hexadecimal representation. Here's an example:

string = "Hello, world!"
hex_string = ''.join([hex(ord(c))[2:] for c in string])

print(hex_string) #Output: 48656c6c6f2c20776f726c6421

The join() Method

This method allows you to join elements of an iterable (such as a list, tuple, or string) into a single string using a separator string.

The syntax for using the join() method is as follows:

separator_string.join(iterable)

Here, separator_string is the string that you want to use to separate the elements in the iterable, and iterable is the sequence of elements that you want to join.

For example, if you have a list of strings and you want to join them into a single string separated by commas, you could use the following code:

my_list = ['mango', 'pineapple', 'banana']
separator = ', '
result = separator.join(my_list)
print(result)  # Output: "mango, pineapple, banana"

In this example, the join() method is called on the separator string, with the my_list list as the iterable argument. The resulting string is assigned to the result variable and printed to the console.

Note that the join() method can also be used with other types of iterables, such as tuples or sets. Additionally, you can use an empty string as the separator if you want to join the elements without any separation.

Contribute with us!

Do not hesitate to contribute to Python tutorials on GitHub: create a fork, update content and issue a pull request.

Profile picture for user almargit
Python developer, content manager.
Profile picture for user AliaksandrSumich
Updated: 03/28/2024 - 22:37
Profile picture for user angarsky
Reviewed and approved