String Manipulations In Python

  • String Concatenation using ‘+’
  • upper()
  • lower()
  • split()
  • str()
  • len()
  • format()
  • Substitution and Date Time
str1 = "Some text"  str2 = " Some other text"  concatenate_str = "Concatenation: " + str1 + str2  print(concatenate_str)    format_str = "firs the tab \t then the new line \nthis format takes effect only after using in \'print\' \  inline break which isn't \"line break\" ".lower()    cap_str = "\ncapitalized".upper()    print(format_str)    print(cap_str)    print("-----------------")    some_text = "Splitting to get words, or with comma"    print(some_text.split())    print(some_text.split(","))    print("Length of some_text: " + str(len(some_text)))    print("\n-----------------")    print("SUBSTITUTION\n")    subst_text = "With {variable} substitution.".format(variable="variable")   print(subst_text)    subst_text = "With {0} {1}.".format("positional", "substitution")   print(subst_text)    subst_text = "With %%s %s." %("substitution")  print(subst_text)    subst_text = "n decimal place float substitution where n=2 here: %.2f" %(20.196343)  print(subst_text)    print("\n-----------------")    print("DATE TIME\n")    import datetime    today = datetime.date.today()  text = '{today.month}/{today.day}/{today.year}'.format(today=today)  print(text)    text = today.strftime("%d/%m/%y")  print(text)    now = datetime.datetime.utcnow() #utc time  text = now.strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]  print(text)    now = datetime.datetime.now() #local time  date_text = now.strftime('%Y/%m/%d %H:%M:%S.%f') #[:-3]  text = "Time is: %s" %(date_text)  print(text)    now = datetime.datetime.now()  date_text = now.strftime('%B %d, %Y %H:%M:%S.%f %p')  text = "Time is %s" %(date_text)  print(text)    now = datetime.datetime.now()  date_text = now.strftime('%x')  text = "Time is %s" %(date_text)  print(text)

OUTPUT

Concatenation: Some text Some other text
firs the tab        then the new line
this format takes effect only after using in ‘print’ inline break which isn’t “line break”

CAPITALIZED
—————–
[‘Splitting’, ‘to’, ‘get’, ‘words,’, ‘or’, ‘with’, ‘comma’]
[‘Splitting to get words’, ‘ or with comma’]
Length of some_text: 37

—————–
SUBSTITUTION

With variable substitution.
With positional substitution.
With %s substitution.
n decimal place float substitution where n=2 here: 20.20

—————–
DATE TIME

6/24/2017
24/06/17
2017-06-24 15:26:48.856
Time is: 2017/06/24 20:56:48.857028
Time is June 24, 2017 20:56:48.857028 PM
Time is 06/24/17