http://www.skillbrew.com /SkillbrewTalent brewed by the industry itself Common String Methods Pavan Verma Python Programming Essentials @YinYangPavan
© SkillBrew http://skillbrew.com Common String Methods  upper()  lower()  capitalize()  startswith()  endswith()  strip()  find()  split()  join() 2
© SkillBrew http://skillbrew.com upper, lower, capitalize 3 message = "enter the dragon" print message.upper() print message.lower() print message.capitalize() Outputs: ENTER THE DRAGON enter the dragon Enter the dragon upper(): Converts all lowercase letters in string to uppercase lower(): Converts all uppercase letters in string to lowercase capitalize(): Capitalizes first letter of string
© SkillBrew http://skillbrew.com startswith 4 startswith(text) • Checks whether the string starts with text • Return’s True if there is a match otherwise False >>> str = "explicit is better than implicit" >>> str.startswith('explicit') True >>> str.startswith('Explicit') False >>>
© SkillBrew http://skillbrew.com startswith (2) 5 str.startswith(text, begin, end) startswith()optionally takes two arguments begin and end • text − This is the string to be checked • begin − This is the optional parameter to set start index of the matching boundary • end − This is the optional parameter to set start index of the matching boundary
© SkillBrew http://skillbrew.com startswith (3) 6 str = "explicit is better than implicit“ >>> print str.startswith("is", 9) True >>> print str.startswith("better", 12, 18) True startswith()returns True if matching string found else False
© SkillBrew http://skillbrew.com endswith 7 >>> str = "explicit is better than implicit" >>> str.endswith("implicit") True >>> str.endswith("implicit", 20) True >>> str.endswith("implicit", 25) False >>> str.endswith("than", 19, 23) True endswith(text)works the same way as startswith difference being it checks whether the string ends with text or not
© SkillBrew http://skillbrew.com message = " enter the dragon " print message.strip() Output: enter the dragon message = "--enter the dragon--" print message.strip(‘-’) Output: enter the dragon strip(chars): • returns a copy of the string with leading and trailing characters removed • If chars is omitted or None, whitespace characters are removed strip 8
© SkillBrew http://skillbrew.com message = " enter the dragon " message.lstrip() Output: 'enter the dragon' message = "--enter the dragon--" message.lstrip('-') Output: 'enter the dragon--' lstrip(chars): • returns a copy of the string with leading characters removed • If chars is omitted or None, whitespace characters are removed lstrip 9
© SkillBrew http://skillbrew.com message = " enter the dragon " message.rstrip() Output: ' enter the dragon' message = "--enter the dragon--" message.rstrip('-') Output: '--enter the dragon' rstrip(chars): • returns a copy of the string with trailing characters removed • If chars is omitted or None, whitespace characters are removed rstrip 10
11 SPLITTING AND JOINING STRINGS
© SkillBrew http://skillbrew.com Splitting 12 message = "enter the dragon" print message.split() Output: ['enter', 'the', 'dragon'] split()returns a list of words of the string
© SkillBrew http://skillbrew.com Splitting (2) 13 message = "enter-the-dragon" print message.split('-') Output: ['enter', 'the', 'dragon'] split(delimiter) delimiter: character or characters which we want to use to split the string, by default it will be space
© SkillBrew http://skillbrew.com Joining 14 seq_list = ['enter', 'the', 'dragon'] print ''.join(seq_list) Output: enter the dragon str.join() returns a string in which the string elements of sequence have been joined by str separator
© SkillBrew http://skillbrew.com Joining (2) 15 seq_tuple = ('enter','the','dragon') print '-'.join(seq_tuple) Output: enter-the-dragon
© SkillBrew http://skillbrew.com splitting/joining example 16 Lets say you have a link 'foo.com/forum?filter=comments&num=20' 1. You have to separate out the querystring from url 2. You have to then separate out key-value pairs in querystring 3. Now update the filter to views and num to 15 and form the url again
© SkillBrew http://skillbrew.com splitting/joining example (2) 17 >>> link = 'foo.com/forum?filter=comments&num=20' >>> components = link.split('?') >>> components ['foo.com/forum', 'filter=comments&num=20'] >>> url = components[0] >>> qs = components[1] >>> url 'foo.com/forum' >>> qs 'filter=comments&num=20' Step 1 Split the link using '?' as delimiter to separate out url and querystring
© SkillBrew http://skillbrew.com splitting/joining example (3) 18 >>> qs 'filter=comments&num=20' >>>params = qs.split('&') >>> params ['filter=comments', 'num=20'] Step 2 Split the querystring using '&' as delimiter
© SkillBrew http://skillbrew.com splitting/joining example (4) 19 >>> params ['filter=comments', 'num=20'] >>> params[0] = 'filter=views' >>> params[1] = 'num=15' >>> params ['filter=views', 'num=15'] >>> qs = '&'.join(params) >>> qs 'filter=views&num=15' Step 3 Update the parameters and join them using '&' as delimiter to form querystring
© SkillBrew http://skillbrew.com splitting/joining example (5) 20 >>> url 'foo.com/forum' >>> qs 'filter=views&num=15' >>> '?'.join([url, qs]) 'foo.com/forum?filter=views&num=15' >>> link = '?'.join([url, qs]) >>> link 'foo.com/forum?filter=views&num=15' Step 4 join url and querystring using '?' as delimiter
Summary  upper, lower, capitalize  startswith and endswith  strip, lstrip and rstrip  splitting and joining strings 21
© SkillBrew http://skillbrew.com Resources  String methods python docs http://docs.Python.org/2/library/stdtypes.html#string- methods 22
23

Python Programming Essentials - M8 - String Methods

  • 1.
    http://www.skillbrew.com /SkillbrewTalent brewed bythe industry itself Common String Methods Pavan Verma Python Programming Essentials @YinYangPavan
  • 2.
    © SkillBrew http://skillbrew.com CommonString Methods  upper()  lower()  capitalize()  startswith()  endswith()  strip()  find()  split()  join() 2
  • 3.
    © SkillBrew http://skillbrew.com upper,lower, capitalize 3 message = "enter the dragon" print message.upper() print message.lower() print message.capitalize() Outputs: ENTER THE DRAGON enter the dragon Enter the dragon upper(): Converts all lowercase letters in string to uppercase lower(): Converts all uppercase letters in string to lowercase capitalize(): Capitalizes first letter of string
  • 4.
    © SkillBrew http://skillbrew.com startswith 4 startswith(text) •Checks whether the string starts with text • Return’s True if there is a match otherwise False >>> str = "explicit is better than implicit" >>> str.startswith('explicit') True >>> str.startswith('Explicit') False >>>
  • 5.
    © SkillBrew http://skillbrew.com startswith(2) 5 str.startswith(text, begin, end) startswith()optionally takes two arguments begin and end • text − This is the string to be checked • begin − This is the optional parameter to set start index of the matching boundary • end − This is the optional parameter to set start index of the matching boundary
  • 6.
    © SkillBrew http://skillbrew.com startswith(3) 6 str = "explicit is better than implicit“ >>> print str.startswith("is", 9) True >>> print str.startswith("better", 12, 18) True startswith()returns True if matching string found else False
  • 7.
    © SkillBrew http://skillbrew.com endswith 7 >>>str = "explicit is better than implicit" >>> str.endswith("implicit") True >>> str.endswith("implicit", 20) True >>> str.endswith("implicit", 25) False >>> str.endswith("than", 19, 23) True endswith(text)works the same way as startswith difference being it checks whether the string ends with text or not
  • 8.
    © SkillBrew http://skillbrew.com message= " enter the dragon " print message.strip() Output: enter the dragon message = "--enter the dragon--" print message.strip(‘-’) Output: enter the dragon strip(chars): • returns a copy of the string with leading and trailing characters removed • If chars is omitted or None, whitespace characters are removed strip 8
  • 9.
    © SkillBrew http://skillbrew.com message= " enter the dragon " message.lstrip() Output: 'enter the dragon' message = "--enter the dragon--" message.lstrip('-') Output: 'enter the dragon--' lstrip(chars): • returns a copy of the string with leading characters removed • If chars is omitted or None, whitespace characters are removed lstrip 9
  • 10.
    © SkillBrew http://skillbrew.com message= " enter the dragon " message.rstrip() Output: ' enter the dragon' message = "--enter the dragon--" message.rstrip('-') Output: '--enter the dragon' rstrip(chars): • returns a copy of the string with trailing characters removed • If chars is omitted or None, whitespace characters are removed rstrip 10
  • 11.
  • 12.
    © SkillBrew http://skillbrew.com Splitting 12 message= "enter the dragon" print message.split() Output: ['enter', 'the', 'dragon'] split()returns a list of words of the string
  • 13.
    © SkillBrew http://skillbrew.com Splitting(2) 13 message = "enter-the-dragon" print message.split('-') Output: ['enter', 'the', 'dragon'] split(delimiter) delimiter: character or characters which we want to use to split the string, by default it will be space
  • 14.
    © SkillBrew http://skillbrew.com Joining 14 seq_list= ['enter', 'the', 'dragon'] print ''.join(seq_list) Output: enter the dragon str.join() returns a string in which the string elements of sequence have been joined by str separator
  • 15.
    © SkillBrew http://skillbrew.com Joining(2) 15 seq_tuple = ('enter','the','dragon') print '-'.join(seq_tuple) Output: enter-the-dragon
  • 16.
    © SkillBrew http://skillbrew.com splitting/joiningexample 16 Lets say you have a link 'foo.com/forum?filter=comments&num=20' 1. You have to separate out the querystring from url 2. You have to then separate out key-value pairs in querystring 3. Now update the filter to views and num to 15 and form the url again
  • 17.
    © SkillBrew http://skillbrew.com splitting/joiningexample (2) 17 >>> link = 'foo.com/forum?filter=comments&num=20' >>> components = link.split('?') >>> components ['foo.com/forum', 'filter=comments&num=20'] >>> url = components[0] >>> qs = components[1] >>> url 'foo.com/forum' >>> qs 'filter=comments&num=20' Step 1 Split the link using '?' as delimiter to separate out url and querystring
  • 18.
    © SkillBrew http://skillbrew.com splitting/joiningexample (3) 18 >>> qs 'filter=comments&num=20' >>>params = qs.split('&') >>> params ['filter=comments', 'num=20'] Step 2 Split the querystring using '&' as delimiter
  • 19.
    © SkillBrew http://skillbrew.com splitting/joiningexample (4) 19 >>> params ['filter=comments', 'num=20'] >>> params[0] = 'filter=views' >>> params[1] = 'num=15' >>> params ['filter=views', 'num=15'] >>> qs = '&'.join(params) >>> qs 'filter=views&num=15' Step 3 Update the parameters and join them using '&' as delimiter to form querystring
  • 20.
    © SkillBrew http://skillbrew.com splitting/joiningexample (5) 20 >>> url 'foo.com/forum' >>> qs 'filter=views&num=15' >>> '?'.join([url, qs]) 'foo.com/forum?filter=views&num=15' >>> link = '?'.join([url, qs]) >>> link 'foo.com/forum?filter=views&num=15' Step 4 join url and querystring using '?' as delimiter
  • 21.
    Summary  upper, lower,capitalize  startswith and endswith  strip, lstrip and rstrip  splitting and joining strings 21
  • 22.
    © SkillBrew http://skillbrew.com Resources String methods python docs http://docs.Python.org/2/library/stdtypes.html#string- methods 22
  • 23.