#username of MySQL: root
#Password: root
#Hostname: localhost
# Database Name in MySQL: test
import mysql.connector
x = mysql.connector.connect(user='root', password='root',host='localhost',database='test')
# prepare a cursor object using cursor() method
cursor = x.cursor()
# execute SQL query using execute() method.
cursor.execute("SELECT VERSION()")
# Fetch a single row using fetchone() method.
data = cursor.fetchone()
print ("Database version : %s " % data)
# disconnect from server
x.close()
Solve Problems by Coding Solutions - A Complete solution for python programming
Database connection test to MySQL
#username of MySQL: root
#Password: root
#Hostname: localhost
# Database Name in MySQL: test
import mysql.connector
x = mysql.connector.connect(user='root', password='root',host='localhost',database='test')
x.close()
#Password: root
#Hostname: localhost
# Database Name in MySQL: test
import mysql.connector
x = mysql.connector.connect(user='root', password='root',host='localhost',database='test')
x.close()
Reversal of a string
startmsg = "hello"
endmsg = ""
for i in range(0,len(startmsg)):
endmsg = startmsg[i] + endmsg
print (endmsg)
endmsg = ""
for i in range(0,len(startmsg)):
endmsg = startmsg[i] + endmsg
print (endmsg)
String replacing with another string with occurrence count
s='brown box red pencil brown pen red pen red box'
print (s)
s1=s.replace('red','brown')
print (s1)
s='brown box red pencil brown pen red pen red box'
print (s)
s1=s.replace('brown','red',1)
print (s1)
s='brown box red pencil brown pen red pen red box'
print (s)
s1=s.replace('brown','red',2)
print (s1)
s='brown box red pencil brown pen red pen red box'
print (s)
s1=s.replace('brown','red',3)
print (s1)
print (s)
s1=s.replace('red','brown')
print (s1)
s='brown box red pencil brown pen red pen red box'
print (s)
s1=s.replace('brown','red',1)
print (s1)
s='brown box red pencil brown pen red pen red box'
print (s)
s1=s.replace('brown','red',2)
print (s1)
s='brown box red pencil brown pen red pen red box'
print (s)
s1=s.replace('brown','red',3)
print (s1)
Searching the pattern in a String using find and index
try:
s="brown box red pencil brown pen red pen red box"
print s.find("red")
print s.find("red",13,len(s))
print s.find("red",34,len(s))
print s.find("crow")
print s.index("red")
print s.index("red",13,len(s))
print s.index("red",34,len(s))
print s.index("crow")
except ValueError:
print "crow not a pattern in string"
s="brown box red pencil brown pen red pen red box"
print s.find("red")
print s.find("red",13,len(s))
print s.find("red",34,len(s))
print s.find("crow")
print s.index("red")
print s.index("red",13,len(s))
print s.index("red",34,len(s))
print s.index("crow")
except ValueError:
print "crow not a pattern in string"
Subscribe to:
Posts (Atom)