Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.
Python optimization.
Hello I was reading up on some Python Documentation. I saw in the beginning of the manual a section for strings. I saw a cool little way how to display the strings and their lengths so I made a little function to display it just like in the document. I was wondering If I covered it in the best way of efficiency. Here is my code:
def gridIt(theString):
a = '+---'
b = '|'
sp = ' '
temp = ''
nums = ''
grid = a*len(theString) + '+'
print grid
for i in range(0,len(theString)):
temp = temp + b + sp + theString[i] + sp
if len(str(i)) == 2:
nums = nums + str(i) + sp*2
else:
nums = nums + str(i) + sp*3
output = temp + b
print output
print grid
print nums + str(len(theString))
x = raw_input("Enter in some words(numbers): ")
gridIt(str(x))
-Thanks
Updated, making functions and using raw_input():
def start():
x = raw_input("Enter in some words( or numbers): ")
gridIt(str(x))
def gridIt(theString):
a = '+---'
b = '|'
sp = ' '
temp = ''
nums = ''
numsr = ''
grid = a*len(theString) + '+'
lenStr = len(theString)
print grid
for i in range(0,lenStr):
temp = temp + b + sp + theString[i] + sp
if len(str(i)) == 2:
nums = nums + str(i) + sp*2
else:
nums = nums + str(i) + sp*3
output = temp + b
print output
print grid
print nums
for j in reversed(xrange(0,lenStr)):
if len(str(j)) == 2:
numsr = numsr + '-' + str(j) + sp
elif j == 0:
numsr = numsr
else:
numsr = numsr + '-' + str(j) + sp*2
print numsr
print "Ther are: " + str(lenStr) + " chars in the string."
start()