1 string_num = "10"
2 print("Type before conversion", type(string_num)) # Output: Type before conversion <class 'str'>
3 int_num = int(string_num) # string conversion to integer
4 print("Type after conversion", type(int_num)) # Output: Type after conversion <class 'int'>
5 print("String value", string_num, "to int :", int_num) # Output: String value 10 to int : 10
6 print("Boolean value True to integer", int(True)) # Output: Boolean value True to integer 1
7 print("Float value 2.5 to integer :", int(2.5)) # Output: Float value 2.5 to integer : 2
2 Float Casting
This is used to convert a value to a float. A float() function is used to do the explicit conversion and returns a float value thus with a decimal point. The argument, that is the value to be converted, is put into the parenthesis. See the below example.
1 string_num = "10.5"
2 print("Type before conversion", type(string_num)) # Output: Type before conversion <class 'str'>
3 float_num = float(string_num)
4 print("Type after conversion", type(float_num)) # Output: Type after conversion <class 'float'>
5 print("String value", string_num, "to float :", float_num) # Output: String value 10.5 to float : 10.5
6 print("Integer value 3 to float :", float(3)) # Output: Integer value 3 to float : 3.0
7 print("Boolean value False to float :", float(False)) # Output: Boolean value False to float : 0.0
3 String Casting
String casting is used to convert a number or any other data type to a string. This is done using the str() function which returns the string representation of the value. The argument, that is the value to be converted, is put into the parenthesis.
1 num = 10
2 print("Type before conversion", type(num)) # Output: Type before conversion <class 'int'>
3 string_num = str(num)
4 print("Integer value ", string_num, "to string: ", string_num) # Output: Integer value 10 to string: 10
5 print("Type after conversion", type(string_num)) # Output: Type after conversion <class 'str'>
6 print("Float value 25.6 to string :", str(25.6)) # Output: Float value 25.6 to string : 25.6
7 print("Boolean value True to string :", str(True)) # Output: Boolean value True to string : True'
4 Boolean Casting
This is used to convert any data type to a Boolean value using the bool() function. Any non-empty string, array, list, dictionary and non-zero is converted to True and the reverse is true. The argument, that is the value to be converted, is put into the parenthesis.
1 num1 = 1
2 print("Type before conversion", type(num1)) # Output: Type before conversion <class 'int'>
3 bool_num1 = bool(num1)
4 print("Int value ", num1, "to bool :", bool_num1) # Output: Int value 1 to bool : True
5 print("Type after conversion", type(bool_num1)) # Output: Type after conversion <class 'bool'>
6 print("0 to boolean :", bool(0)) # Output: 0 to boolean : False
7 print("A non-empty tuple to boolean :", bool((5))) # Output: A non-empty tuple to boolean : True
8 print("Empty list to boolean :", bool([])) # Output: Empty list to boolean : False
5 List Casting
This is used to convert any iterable data type to a list using the list() function. The argument, that is the value to be converted, is put into the parenthesis.
In line 2 below, the string "hello" is converted to a list of individual characters. Remember that [] represents the list as shown on the output. As shown in line 8 below, converting a dictionary to a list returns the list of dictionary keys.
1 string = "hello"
2 print(string, " type before conversion", type(string)) # Output : hello type before conversion <class 'str'>
3 list_string = list(string)
4 print(list_string, " type after conversion", type(list_string)) # Output : ['h', 'e', 'l', 'l', 'o'] type after conversion <class 'list'>
5 print("A string", string, "to list: ", list_string) # Output: A string hello to list: ['h', 'e', 'l', 'l', 'o']
6 print("A set to list :", list({1, 6, 9, 8})) # Output: A set to list : [8, 1, 6, 9]
7 print("A tuple to list :", list((1, 6, 9, 8))) # Output: A tuple to list : [1, 6, 9, 8]
8 print("A dictionary to list :", list({'a': 1, 'b': 2, 'c': 3, 'd': 4})) # Output: A dictionary to list : ['a', 'b', 'c', 'd']
6 Tuple casting
This is used to convert any iterable data type to a tuple using the tuple() function. The argument, that is the value to be converted, is put into the parenthesis. Strings are converted to a tuple of individual characters.
Remember that () represents a tuple as shown on output. As shown in line 8 below, converting a dictionary to a list returns the tuple of dictionary keys.
1 list_num = [1, 2, 3]
2 print(list_num, " type before conversion", type(list_num)) # Output : [1, 2, 3] type before conversion <class 'list'>
3 tuple_num = tuple(list_num)
4 print(tuple_num, " type after conversion", type(tuple_num)) # Output : (1, 2, 3) type after conversion <class 'tuple'>
5 print("A list", list_num, "to tuple: ", tuple_num) # Output: A list [1, 2, 3] to tuple: (1, 2, 3)
5 print("A string to tuple: ", tuple("Hello")) # Output: A string to tuple: ('H', 'e', 'l', 'l', 'o')
6 print("A set to tuple :", tuple({1, 6, 9, 8})) # Output: A set to tuple : (8, 1, 6, 9)
7 print("A dictionary to tuple :", tuple({'a': 1, 'b': 2, 'c': 3, 'd': 4})) # Output: A dictionary to tuple : ('a', 'b', 'c', 'd')
7 Set Casting
This is used to convert any iterable data type to a set using the set() function. Strings are converted to a list of individual characters. The argument, that is the value to be converted, is put into the parenthesis. Remember that {} represents a set as shown on output.
As shown in line 8 below, converting a dictionary to a set returns the set of dictionary keys. Another point to note is that the set does not support repeated elements, it returns unique elements. The output of line 6 is a set with a single "l" though "Hello" has a double "l".
This powerful feature becomes useful when we want to eliminate repeating elements. Also, in a set, no element has a permanent position so the output may differ on every run so don't expect your output and mine to be similar, nor be shocked when another run gives a set with elements in a different order
1 list_num = [1, 2, 3]
2 print(list_num, " type before conversion", type(list_num)) # Output : [1, 2, 3] type before conversion <class 'list'>
3 set_num = set(list_num)
4 print(set_num, " type after conversion", type(set_num)) # Output : {1, 2, 3} type after conversion <class 'set'>
5 print("A list", list_num, "to set: ", set_num) # Output: A list [1, 2, 3] to set: {1, 2, 3}
6 print("A string to set: ", set("Hello")) # Output: A string to tuple: A string to set: {'o', 'e', 'H', 'l'}
7 print("A tuple to set :", set((1, 6, 9, 8))) # Output: A set to tuple : A tuple to set : {8, 1, 6, 9}
8 print("A dictionary to set :", set({'a': 1, 'b': 2, 'c': 3, 'd': 4})) # Output:A dictionary to set : {'d', 'c', 'b', 'a'}
8 Dictionary Casting
This is used to convert a list of key-value pairs to a dictionary using the dict() function. The argument, that is the value to be converted, is put into the parenthesis. Any iterable in another iterable may be used to create a dictionary, with the first value as a list and the second as a value.
In this example, the list [("a", 1), ("b", 2)] with nested tuple is converted to a dictionary
1 list_kv = [("a", 1), ("b", 2)]
2 print(list_kv, " type before conversion", type(list_kv)) # Output : [('a', 1), ('b', 2)] type before conversion <class 'list'>
3 dict_kv = dict(list_kv)
4 print(dict_kv, " type after conversion :", type(dict_kv)) # Output :{'a': 1, 'b': 2} type after conversion <class 'dict'>
5 print("A list", list_kv, " to dictionary : ", dict_kv) # Output: A list [('a', 1), ('b', 2)] to dictionary : {'a': 1, 'b': 2}
6 print("List of nested tuples to dictionary :", dict([(1, "a"), (2, "b")])) # Output: List of nested tuples to dictionary : {1: 'a', 2: 'b'}
7 print("Tuple of nested lists to dictionary :", dict((["a", 1], ["b", 2]))) # Output: Tuple of nested lists to dictionary : {'a': 1, 'b': 2}
Product of
Jeremiah Taguta T/A Charis Technologies