Python - string


string is defined a sequence of characters enclosed within single or double quotes.
Python has no support for character types like C/C++, C#, Java etc.
So we need to use a single character string for defining any character.



  """
      created_at       : 16 Feb 2017.
      updated_at       : 16 Feb 2017.
      aim of script    : To understand the concept of strings in Python.
      coded by         : Rishikesh Agrawani.
      Python version   : 2.7.12
  """
  

 #Interactive mode programming - #1

>>> name = "Julia Kelly" #with double quotes >>> city = 'Bangalore' #with single quotes >>> >>> type(name) #printing type <type 'str'> >>> >>> type(city) #printing type <type 'str'> >>> >>> print name,city #printing name & city Julia Kelly Bangalore >>> >>> print name #printing name Julia Kelly >>> >>> print city #printing city Bangalore >>> >>> print "%s -- %s"%(name,city) #1st way of formatting string variables Julia Kelly -- Bangalore >>> >>> print "{} == {}".format(name,city) #2nd way of formatting string variables Julia Kelly == Bangalore >>>

 #Interactive mode programming - #2

>>> firstname = 'Dolly' >>> lastname = "Heldon" >>> >>> print (firstname) Dolly >>> >>> print firstname Dolly >>> >>> print firstname+lastname DollyHeldon >>> >>> print firstname+"\t"+lastname Dolly Heldon >>> >>> print firstname+"\n"+lastname Dolly Heldon >>> >>> len(firstname) 5 >>> len(lastname) 6 >>>

 #Interactive mode programming - #3 (using some basic escape characters)

>>> print("Hello\nWorld") #using \n Hello World >>> print("Hello\tWorld") #using \t Hello World >>> print("Hello\bWorld") #using \b HellWorld >>> print("Hello\rWorld") #using \r World >>> print("Hellok\rWorld") #using \r Worldk >>> print("Hellok\fWorld") #using \f Hellok World >>> print("Hellok\fWorld\fPython") #using \f Hellok World Python >>> >>> print "Hello",chr(0x0a),"World\fRishikesh" #0x0a is hex representation Hello World Rishikesh >>> >>> city = "Bangalore" >>> print city + " is cool place to survive.\vIt is in India." #using \v Bangalore is cool place to survive. It is in India. >>> >>> print city + " is cool place to survive.\v\vIt is in India." #using \v Bangalore is cool place to survive. It is in India. >>>

 #Interactive mode programming - #3 (indexing & slicing)

>>> s = "ABCDEFGHIJ" >>> print s[0] #Accessing characters using index A >>> print s[len(s)-1] #Accesssing last character of a string J >>> id(s) 4558293776 >>> >>> s2 = s >>> id(s2) 4558293776 >>> >>> print s2 ABCDEFGHIJ >>> >>> s3 = s[:] #getting a slice having all the characters of string >>> id(s3) 4558293776 >>> >>> print s ABCDEFGHIJ >>> >>> print s[:2] #getting a substing having characters from start to 2nd character AB >>> print s[:3] #getting a substing having characters from start to 3rd character ABC >>> print s[:len(s)] #getting a substing having characters from start to end ABCDEFGHIJ >>> >>> print s[1:len(s)] #getting a substing having characters at index 1 to end BCDEFGHIJ >>> >>> print s[1:] #Same as above BCDEFGHIJ >>> print s[3:] #getting a substing having characters at index 3 to end DEFGHIJ >>>

 #Interactive mode programming - #4 (string operations)

>>> s = "ABCDEFGHIJ" >>> >>> print s ABCDEFGHIJ >>> >>> print len(s) #len() function 10 >>> >>> id(s) #id() function 4558293776 >>> >>> s.replace("AB","ab") #replace() method 'abCDEFGHIJ' >>> >>> print s ABCDEFGHIJ >>> >>> s2 = 'ABxyABpqABmnABcdAB' >>> >>> s2.replace("AB","##",2) #3rd parameter denotes the max number of replacements '##xy##pqABmnABcdAB' >>> >>> s2.replace("AB","##",3) '##xy##pq##mnABcdAB' >>> >>> s2.replace("AB","##",4) '##xy##pq##mn##cdAB' >>> >>> s2.replace("AB","##",5) '##xy##pq##mn##cd##' >>> >>> s2.replace("AB","##",6) '##xy##pq##mn##cd##' >>>

 #Interactive mode programming - #5 (formatting)

>>> name = 'Smiley Moullen' >>> age = 25 >>> salary = 80000.567 >>> >>> print "My name is",name,".I am ",age,' .I earn',salary,'monthly.' My name is Smiley Moullen .I am 25 .I earn 3.14 monthly. >>> >>> print "My name is %s .I am %d .I earn %f monthly."%(name,age,salary) My name is Smiley Moullen .I am 25 .I earn 80000.567000 monthly. >>> >>> print "My name is %s .I am %d .I earn %.3f monthly."%(name,age,salary) My name is Smiley Moullen .I am 25 .I earn 80000.567 monthly. >>> >>> print "My name is %s .I am %d .I earn %.2f monthly."%(name,age,salary) My name is Smiley Moullen .I am 25 .I earn 80000.56 monthly. >>> >>> s = "abcd" >>> s2 = 'abCdEf' >>> s3 = 'aBcD@tIGer#' >>> >>> s.capitalize() #Capitalie the first letter 'Abcd' >>> >>> s2.capitalize() 'Abcdef' >>> s3.capitalize() 'Abcd@tiger#' >>> >>> s.isalpha() #checks whether the string only contains alphabetic characters or not True >>> s3.isalpha() False >>> >>> s4='12345' >>> s4.isdigit() #checks whether the string only contains numeric characters True >>> s.isdigit() False >>> >>> s5 = u"abcd123" >>> s6 = u'1234' >>> >>> s5.isnumeric() False >>> s6.isnumeric() True >>> s.isnumeric() Traceback (most recent call last): File "", line 1, in AttributeError: 'str' object has no attribute 'isnumeric' >>> >>> print s3 aBcD@tIGer# >>> >>> print s3.upper() #returns uppercased string ABCD@TIGER# >>> >>> s = 'ABCD123' >>> print s.lower() #returns lowercased string abcd123 >>> >>> print s.upper() ABCD123 >>> >>> n = u"1234" >>> s = u'1234abc' >>> s2 = "1234abc" >>> n.isdecimal() #checks whether unicode string only contains decimal digits or not True >>> s.isdecimal() False >>> s2.isdecimal() Traceback (most recent call last): File "", line 1, in AttributeError: 'str' object has no attribute 'isdecimal' >>>