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

String format converting

Here are some examples of how to perform common data type conversions in Python:

  • Converting a string to bytes:
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.

  • Converting a hex string to an integer:
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.

  • Converting a base-2 binary number 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.

  • Converting bytes to a hex string:
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.

  • Converting an integer to a 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.

  • Converting an integer to a string with a base:
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.

  • Converting a string with commas in it as thousands separators to a number:
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.

  • Converting all strings in a list to integers:
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.

  • Converting a tuple of tuples string to a list of lists integers:
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.

  1. String interpolation in Python:

    • Description: String interpolation is the process of substituting values into a string. In Python, this can be done using various techniques.
    • Example Code:
      name = "Alice"
      age = 30
      interpolated_string = f"My name is {name} and I am {age} years old."
      
  2. Formatting variables in Python strings:

    • Description: Format variables within strings using the % operator or the .format() method.
    • Example Code:
      name = "Bob"
      age = 25
      formatted_string = "Hello, my name is %s and I am %d years old." % (name, age)
      
  3. Converting data types in string formatting:

    • Description: Convert data types within formatted strings using % or .format() with appropriate format specifiers.
    • Example Code:
      value = 3.14159
      formatted_string = "The value is %.2f" % value
      
  4. Using placeholders in Python string format:

    • Description: Placeholders like {} can be used in strings and later filled with values using .format() method.
    • Example Code:
      name = "Charlie"
      age = 22
      formatted_string = "Name: {}, Age: {}".format(name, age)
      
  5. String format specifiers in Python:

    • Description: Format specifiers define how variables should be formatted within a string. Common ones include %s, %d, %f, etc.
    • Example Code:
      name = "David"
      age = 28
      formatted_string = "Name: %s, Age: %d" % (name, age)
      
  6. F-strings and format() method in Python:

    • Description: F-strings (formatted string literals) and .format() method are modern ways of string formatting in Python 3.
    • Example Code:
      name = "Eva"
      age = 35
      f_string = f"Name: {name}, Age: {age}"
      format_method_string = "Name: {}, Age: {}".format(name, age)
      
  7. Formatting floats and decimals in Python strings:

    • Description: Floats and decimals can be formatted using format specifiers to control precision and formatting.
    • Example Code:
      value = 123.456789
      formatted_float = "Formatted value: {:.2f}".format(value)
      
  8. Customizing string output with format options:

    • Description: String formatting options like alignment, padding, and width can be customized for better output.
    • Example Code:
      message = "Hello"
      formatted_message = "{:^20}".format(message)  # Centered within 20 characters