create a matrix from the size of the length of two strings in python

You can create a matrix from the length of two strings in Python using nested lists. Here is an example code snippet that takes two strings as input, calculates their lengths, and creates a matrix with the size of the length of the two strings:

main.py
string1 = input("Enter string 1: ")
string2 = input("Enter string 2: ")

# Calculate the lengths of the strings
len1 = len(string1)
len2 = len(string2)

# Create a matrix with the size of the length of the two strings
matrix = [[0 for j in range(len2)] for i in range(len1)]

# Print the matrix
for row in matrix:
    print(row)
329 chars
14 lines

In this code, we first take two strings as input using the input() function. Then, we calculate the lengths of the strings using the len() function and store them in the variables len1 and len2.

Next, we create a matrix with the size of the length of the two strings using a nested list comprehension. The outer loop creates a list of length len1 and the inner loop creates a list of length len2, with each element initialized to 0.

Finally, we print the matrix using a loop that iterates over the rows of the matrix and prints each row.

gistlibby LogSnag