Python Tutorial
Python Flow Control
Python Functions
Python Data Types
Python Date and Time
Python Files
Python String
Python List
Python Dictionary
Python Variable
Python Input/Output
Python Exceptions
Python Advanced
Here are some examples of how to perform common data type conversions in Python:
my_string = "hello" my_bytes = my_string.encode() print(my_bytes) # prints b'hello'
In this example, we use the encode
method to convert the string my_string
to bytes.
my_hex_string = "7F" my_int = int(my_hex_string, 16) print(my_int) # prints 127
In this example, we use the int
function with a base of 16 to convert the hex string my_hex_string
to an integer.
my_binary_string = "1010" my_int = int(my_binary_string, 2) print(my_int) # prints 10
In this example, we use the int
function with a base of 2 to convert the binary string my_binary_string
to an integer.
my_bytes = b'\x00\xFF\xA5' my_hex_string = my_bytes.hex() print(my_hex_string) # prints 00ffa5
In this example, we use the hex
method to convert the bytes my_bytes
to a hex string.
my_int = 42 my_string = str(my_int) print(my_string) # prints "42"
In this example, we use the str
function to convert the integer my_int
to a string.
my_int = 42 my_string = format(my_int, 'b') print(my_string) # prints "101010"
In this example, we use the format
function with a format specifier of 'b'
to convert the integer my_int
to a binary string.
my_string = "1,000,000" my_int = int(my_string.replace(",", "")) print(my_int) # prints 1000000
In this example, we use the replace
method to remove the commas from the string my_string
, and then convert it to an integer using the int
function.
my_list = ["1", "2", "3"] my_int_list = [int(s) for s in my_list] print(my_int_list) # prints [1, 2, 3]
In this example, we use a list comprehension to convert each string in my_list
to an integer using the int
function.
my_tuple = (("1", "2", "3"), ("4", "5", "6"), ("7", "8", "9")) my_list = [[int(s) for s in t] for t in my_tuple] print(my_list) # prints [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
In this example, we use nested list comprehensions to convert each string in each tuple in my_tuple
to an integer using the int
function, and then convert the tuple of tuples to a list of lists int.
String interpolation in Python:
name = "Alice" age = 30 interpolated_string = f"My name is {name} and I am {age} years old."
Formatting variables in Python strings:
%
operator or the .format()
method.name = "Bob" age = 25 formatted_string = "Hello, my name is %s and I am %d years old." % (name, age)
Converting data types in string formatting:
%
or .format()
with appropriate format specifiers.value = 3.14159 formatted_string = "The value is %.2f" % value
Using placeholders in Python string format:
{}
can be used in strings and later filled with values using .format()
method.name = "Charlie" age = 22 formatted_string = "Name: {}, Age: {}".format(name, age)
String format specifiers in Python:
%s
, %d
, %f
, etc.name = "David" age = 28 formatted_string = "Name: %s, Age: %d" % (name, age)
F-strings and format() method in Python:
.format()
method are modern ways of string formatting in Python 3.name = "Eva" age = 35 f_string = f"Name: {name}, Age: {age}" format_method_string = "Name: {}, Age: {}".format(name, age)
Formatting floats and decimals in Python strings:
value = 123.456789 formatted_float = "Formatted value: {:.2f}".format(value)
Customizing string output with format options:
message = "Hello" formatted_message = "{:^20}".format(message) # Centered within 20 characters