str()

Convetring object to string.
str(object,encode='utf-8',errors='strict')
object : ( Required ) The object which is to be coverted to string
encode : ( Optional ) default is 'utf-8'
errors : ( Optional ) what to do if error occurs.
print(str(3.4)) #  3.4
Using type
print(type(str(3.4))) #  <class 'str'>
Using bytes
my_str = "plus2net.com"
# encoding 'utf-8'
my_bytes = bytes(my_str, 'utf-8')
print(my_bytes) #  b'plus2net.com'
print(type(my_bytes)) #  <class 'bytes'>
print(str(my_bytes, encoding='ascii', errors='ignore'))# plus2net.com

Data type of string object

We used type() to get data type of any string object.
my_str='plus2net'
print(type(my_str)) # <class 'str'>

Example 1: Convert a Float to a String

pi = 3.14159
print(str(pi))  # Output: '3.14159'

Example 2: Convert a List to a String

my_list = [1, 2, 3]
print(str(my_list))  # Output: '[1, 2, 3]'

Example 3: Convert a Boolean to a String

is_true = True
print(str(is_true))  # Output: 'True'

Example 4: Convert a Complex Number to a String

complex_num = 3 + 4j
print(str(complex_num))  # Output: '(3+4j)'

Example 5: Convert a Dictionary to a String

my_dict = {'name': 'Alice', 'age': 30}
print(str(my_dict))  # Output: "{'name': 'Alice', 'age': 30}"

Example 6: Convert a Tuple to a String

my_tuple = (10, 20, 30)
print(str(my_tuple))  # Output: "(10, 20, 30)"

Example 7: Convert None to a String

none_value = None
print(str(none_value))  # Output: 'None'

Example 8: Convert a Set to a String

my_set = {1, 2, 3}
print(str(my_set))  # Output: '{1, 2, 3}'

All Built in Functions in Python repr()


Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer