Strings, Concatenation & Date/Time

Initially published on 23/04/2015 6277916_orig Start with : Post 1 Dot Notation Using dot notation with strings. Let us consider an example where we convert the string to upper case using dot notation. >>> mysore = "The city is awesome !" >>> print len(mysore) >>> print mysore.upper() Printing strings >>> print("This String") Printing Variables >>> Stone_cold = "Stunner" >>> print(Stone_cold) Concatenation Combining strings together is called concatenation The + operator combine the strings. >>> print "Indian "+ "Air " +"Force " The space after the word is to make the string look like sentence. String Conversion The str() method converts non-strings into strings. >>> print "Sourav Ganguly scored "+ str(100) + "on his Test Debut" String Formatting The % operator after a string combines a string with variables. The % operator will replace  %s in the string with the string variable. Example 1: >>> string_1 = "Sourav" >>> string_2 = "Dada" >>> print "Indian captain %s is also called as %s." % (string_1, string_2) Example 2: >>> batsman = raw_input("Who is the batsman?") >>> captain = raw_input("Who is the captain?") >>> series = raw_input("Which series did he win for India?") >>> print "The Indian %s and %s Sourav Ganguly won %s series for India." % (batsman, captain, series) Date and Time  We can import datetime function from datetime module. >>> from datetime import datetime Current Date and Time Here is the code to print current date and time. >>> from datetime import datetime >>> now = >>> print (* The above exercises are available at codeacademy)

Leave a comment

Please note, comments must be approved before they are published