Reverse characters

Program for reversing first three characters and next three characters in a word with even length. word = input(‘Enter word: ‘) if len(word)%2==0 and len(word) >= 6 : r1 = word[0:3] r2 = word[3:6] s1 = r1[::-1] s2 = r2[::-1] print(‘Word after reversing first 3 letters and next 3 letters:’,s1+s2) Output Enter word: PYTHON WordContinue reading “Reverse characters”

Alphanumeric

# Check type for alphanumeric input s = input(‘Enter alphanumeric string: ‘) if s.isalnum(): print(‘Input is alphanumeric. ‘) print(‘Data type: ‘,type(s)) else: print(‘Input is not alphanumeric.’) print(‘Your input: ‘,s) Output Enter alphanumeric string: a123a Input is alphanumeric. Data type: str Your input: a123a

MySQL resultset into Json

from mysql import connector import json connection = connector.connect(host = ‘localhost’, database = ‘testdb’, user = ‘root’, password = ‘root’) cursor = connection.cursor() cursor.execute(‘select * from product’) products = cursor.fetchall() prodcode=list() prodname=list() for product in products: prodcode.append(product[0]) prodname.append(product[1]) connection.close() result=zip(prodcode,prodname) prod=dict(result) s=json.dumps(prod,separators=(‘,’, ‘:’)) open(‘product.json’,’w’).write(s) Output: {“A100″:”PenDrive”,”A102″:”TV”,”A103″:”BlueRay Player”,”B100″:”Mouse”,”B101″:”Keyboard”,”B102″:”Monitor”,”C300″:”Speakers”}