![]() Semantics (meaning) of these operators is similar to their meaning in English. Same thing is applicable to dictionaries and tuples.There are three logical operators, and, or, and not,īoolean expressions from simpler Boolean expressions. Print(converted_my_list) # ant, soldier, termite To convert the list to a string, you have to use the join() method: my_list = ![]() Well, you don’t get an error if you do that, what you’ll get back is the iterable as it is: my_list = You might be wondering if you can use the str() function to convert iterable data like lists, tuples, and dictionaries to a string. You’ve seen that the str() function is instrumental in converting non-string objects and primitive data types to strings. My_str = my_code(encoding='UTF-8', errors='strict') To decode a bytes to string, you should use the decode() method this way: my_bytes = b'Hello, world!' Print(my_bytes) # Output: b'Hello, world!' My_bytes = my_str.encode(encoding='UTF-8', errors='strict') To encode a string to bytes, for example, you have to use the encoding() method this way: my_str = "Hello world!" The encoding parameter is useful for encoding a string to bytes and decoding a bytes to strings. How to use the encoding Parameter of the str() Function for Encoding and Decoding Objects Print("Converted Boolean is", converted_my_false_bool, "and the type of the result is ", type(converted_my_false_bool)) # Converted Boolean is False and the type of the result is Print("Converted Boolean is", converted_my_true_bool, "and the type of the result is ", type(converted_my_true_bool)) # Converted Boolean is True and the type of the result is You can also convert a Boolean to a string if you want: my_true_bool = TrueĬonverted_my_true_bool = str(my_true_bool)Ĭonverted_my_false_bool = str(my_false_bool) How to Convert a Boolean to String with the str() Function You can see the type of the converted integer and float is a string. Print("Converted float is", converted_my_float, "and the type of the result is ", type(converted_my_float)) # Converted float is 34.8 and the type of the result is Print("Converted integer is", converted_my_int, "and the type of the result is ", type(converted_my_int)) # Converted integer is 45 and the type of the result is ![]() You can also verify that the types of the results are strings with the type() function: my_int = 45 You can convert an integer or float to a string with str() this way: my_int = 45 How to Convert an Integer and Float to String with the str() Function If you’re insistent on using the encoding and errors parameters, then the object to convert must be a bytes object: my_num = b'45'Ĭonverted_my_num = str(my_num, encoding='utf-8', errors='strict') You only need the number you want to convert: my_num = 45 In this case, you don’t need the encoding and errors at all. This error occurs because you’re using the encoding parameter without providing a bytes object. TypeError: decoding to str: need a bytes-like object, int found If you run the code, you’ll get this error: converted_my_num = str(my_num, encoding='utf-8', errors='errors') ![]() You have to comma-separate each of the parameters in the str() function, and the values of both encoding and errors have to be in strings: str(object_to_convert, encoding='encoding', errors='errors')įirst, let’s see how to use all the parameters of the str() function: my_num = 45Ĭonverted_my_num = str(my_num, encoding='utf-8', errors='errors') The values you can use for this parameter include strict, ignore, replace, and others. errors: specifies what to do if decoding fails.encoding: the encoding of the data to convert.If you don’t provide it, str() returns an empty string as the result. object: the data you want to convert to a string.This object the str() function takes can be a float, integer, or even a Boolean.Īpart from the compulsory data to convert to a string, the str() function also takes two other parameters. The str() function takes a compulsory non-string object and converts it to a string. It’s a built-in function you can use to convert any non-string object to a string. One of those functions we’ll look at in this article is str(). The programming language provides several functions you can use to convert any of these data types to the other. Python’s primitive data types include float, integer, Boolean, and string.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |