Cool stuff with Strings

Wed 28 January 2015

Filed under Python

Strings

Here I share some cool things you can do with strings.

count(), len(), max(), min(),

>>> 'ababababbbbababab'.count('a')
7
>>> 'ababababbbbababab'.count('ab')
7
>>> 'ababababbbbababab'.count('bb')
2
>>> 'ababababbbbababab'.count('ab',6)
4
>>> 'ababababbbbababab'.count('ab',6,10)
1


>>> len('afhjcrnamwvsiytdr')
17
>>> max('afhjcrnamwvsiytdr')
'y'
>>> min('fhjcrnmwvsiytdr')
'c'

Syntax str.count(substring[,start[,end]])

  • original_string.count(what_to_count[,where_to_start[,where_to_end]])
  • counts how many times that substring is found in that section of the string
  • returns a number

Syntax len(string), max(string), min(string)

  • len - returns a number - the length of the string
  • max - returns a character - the highest one in the string (numbers are lower than letters)
  • min - returns a character - the lowest one in the string (numbers are lower than letters. And there are some characters that are even lower than some numbers.)

Examples

>>> characters = "`~!@#$%^&*()_-+={}[]|\\:;'<>,.?/123ABC"

>>> min(characters)
'!'
>>> max(characters)
'~'
>>> sorted(characters)
['!', '#', '$', '%', '&', "'", '(', ')', '*', '+', ',', '-', '.', '/', '1', '2', '3', ':', ';', '<', '=', '>', '?', '@', 'A', 'B', 'C', '[', '\\', ']', '^', '_', '`', '{', '|', '}', '~']

That last one has interesting results!


zfill

>>>'123'.zfill(5)
'00123'

Syntax str.zfill(width)

  • returns a numeric string with as many leading zeroes as needed to fill the width in the argument of the zfill().
  • in this case, the zfill() has argument of 5. so there must be 5 digits in the result. if there are less, then it places zeroes in front of the number.
  • in this case the str had 3 digits ('123'), so zfill() added 2 leading zeroes.

join

>>>'*'.join('12345')
'1*2*3*4*5'

Syntax str.join(iterable)

  • returns a string which a concatenation of the str between each iterable.

Examples

>>>'-'.join(['212', '555', '1212'])
'212-555-1212'

>>> ",".join('?'*7)
'?,?,?,?,?,?,?'

replace

>>> 'AB.CD.EF.GH'.replace('.','')
'ABCDEFGH'

Syntax str.replace(old, new[,count])

  • returns a string.
  • original_string.replace(replace_what, replace_with, [how many of those to replace])

Examples

>>> 'AB.CD.EF.GH'.replace('.','')
'ABCDEFGH'

>>> 'AB.CD.EF.GH'.replace('.','',2)
'ABCDEF.GH'
#replaces only the first 2

find / index

>>> 'ABCABDAGH'.find('A')
0

>>> 'ABCABDAGH'.index('A')
0

>>> 'ABCABDAGH'.find('B')
1

>>> 'ABCABDAGH'.index('B')
1

Syntax str.find(sub[,start[,end]]) / str.index(sub[,start[,end]])

  • returns the index of the first substring was found
  • original_string.find(what_to_find, where_to_start_looking, where_to_stop_looking)

  • find - if the substring is not found, returns -1

  • index - will raise ValueError if the substring is not found

Examples

>>> str = 'MISSISSIPPI'

>>> str.find('I')
1  

>>> str.find('IS', 5)
-1  #The start position of 5 means that this search is searching through everything beginning from index of 5 ie: 'SSIPPI' and thus the seach_item is not found. And the result is -1.

>>> str.find('IS', 4) #The start position of 4 means that this search is searching through everything beginning from index of 4 ie: 'ISSIPPI' and thus the seach_item is not found. And the result is index 4 - from the beginning of the original string. 
4

>>> str.find('I',9,12)
10

>>> str.find('I',11,16)
-1

split/ partition (rsplit/ rpartition)

>>> v = '212-555-1212'

>>> v.partition('-')
('212', '-', '555-1212')

>>> v.split('-')
['212', '555', '1212']

>>> v.rpartition('-')
('212-555', '-', '1212')

Syntax str.split([separator[,maxsplit]]) str.partition(separator)

  • split - separates the original_string at every instance of the 'separator'
  • returns a list.
  • if maxsplit is given, then the original_string is only separated that number of times.
  • a separator is not required. if none is given, the algorithm separates at 'empty space' (consecutive spaces)
  • rsplit - begins separating from the right (from the end of the string)

  • partition - separates at the first instance of the separator.

  • returns a 3 part tuple: (before_the_first_separator, the separator, after_the_first_ separator)
  • if the separator is not found, then returns a 3 part tuple:(the original_string, '', '')
  • rpartition does the partition starting from the right.

Examples

>>>w='212 555    1212'

>>>w.split()
['212', '555', '1212']

>>>w.split(None,1)
['212', '555    1212']  # This separates only 1 time. And since the separator is `None`, it uses the empty spaces as a separator.

>>>w.rsplit(None,1)
['212 555', '1212']  # Separates 1 time. Starting from the right.

>>> v.partition('-')
('212', '-', '555-1212')

>>> v.partition('*')
('212-555-1212', '', '')  # The separator is not found.

>>> v.rpartition('-')
('212-555', '-', '1212')

Strip (lstrip(), rstrip())

>>>'   abcd   '.strip()
abcd

Syntax str.strip([characters])

  • strips off the characters - from the beginning and end of the string. until it hits a character that doesn't match the specified character.
  • returns a string
  • if no characters are specificed, this strips off the empty spaces at the beginning and end

  • rstrip() - strips off the characters from the right

  • lstrip() - strips off the characters from the left

Examples

>>>y = '   abcdeabcdeeeef'


>>> y.strip()
'abcdeabcdeeeef'

>>> y.rstrip('e')
'   abcdeabcdeeeef'

>>> y.rstrip('f')
'   abcdeabcdeeee'

>>> y.strip('ef')
'   abcdeabcd'  # strips all the 'e's and 'f' until it finds another character and then stops stripping.

lower(), upper(), title(), swapcase(), capitalize()

NOTE: These do not change the original string. They just display them according to the operation.

>>> 'LESLIE'.lower()  # returns the string as lowercase
'leslie'

>>> 'john'.upper()  # returns the string as uppercase
'JOHN'
>>> 'jason street'.title()  #returns the string as title case - the first letter of every word is capitalized
'Jason Street'

>>> 'rACHEL gREEN'.swapcase()  #returns the string with cases swapped - upper becomes lower and lower becomes upper
'Rachel Green'

>>> 'barry smith'.capitalize() #returns the string with *only* the first letter capitalized
'Barry smith'

center(), ljust(), rjust()

Syntax center(width), ljust(width[,character]), rjust(width[,character])

>>> 'rachel'.center(30)
'            rachel            '  # centers the string in the specified width

>>> 'rachel'.center(30,'-')
'------------rachel------------'  # centers the string in the specified width and pads the empty spaces with the specified character.

>>> 'rachel'.ljust(30)
'rachel                        '  # left justifies (pushes all the way to the left) of the specified width.

>>> 'rachel'.ljust(30,'-') 
'rachel------------------------' # left justifies of the specified width and pads the empty spaces with specified character.


>>> 'rachel'.rjust(30) 
'                        rachel' # right justifies (pushes all the way to the right) of the specified width.

>>> 'rachel'.rjust(30,'-')
'------------------------rachel' # rght justifies of the specified width and pads the empty spaces with specified character.

endswith(), startswith()

>>> '123456789*'.endswith('*')
True

>>>'*1234'.startswith('*')
True
  • string.endswith(what_to_look_for[, index_start_looking[, index_stop_looking]])
  • string.startswith(what_to_look_for[, index_start_looking[, index_stop_looking]])
  • returns True if the string startswith / endswith the substring in that section of the string. returns False if the string in that section does not startswith/ endswith the substring

Examples

>>> '123456789*'.endswith('7', 6, 7)
True

>>> '123456789*'.endswith('7', 7)
False

>>> '123456789*'.endswith('7', 6, 8)
False

>>> '123456789*'.endswith('78', 6, 8)
True

>>> 'ABCDEFGH'.startswith('AB')
True

>>> 'ABCDEFGH'.startswith('B',1)
True

>>> 'ABCDEFGH'.startswith('EF', 4,9)
True

>>> 'ABCDEFGH'.startswith('EF', 5,9)
False

isalnum(), isalpha(), isdigit(), isspace(), islower(), istitle(), isupper()

All these return True or False

>>> '123ABC'.isalnum()
True  #checks if all characters in the string are alphanumeric. And string is not empty.
>>> '*asfd897'.isalnum()
False


>>> '123ABC'.isalpha()
False  # checks if all characters in the string are alphabetic. And string is not empty.

>>> '123ABC'.isdigit()
False  # checks if all characters in the string are numeric. And string is not empty.


>>> '        '.isspace()
True  # checks if all characters in the string are empty spaces. And string is not empty.
>>> ''.isspace()
False


>>> '1234abc'.islower()  # checks if all alphabetic are lower case
True

>>> '1234Abv'.istitle()  #checks if all instances of the alphabetic sequences are  title case.   
True
>>> '1234abv'.istitle()
False
>>> '12As45df'.istitle()
False
>>> '12As45Ddf'.istitle()
True


>>> '1234abv'.isupper() # checks if all alphabetic are lower case
False
>>> '1234ABV'.isupper()
True

Comments


DeeKras.com © Dee Kras Powered by Pelican and Twitter Bootstrap. Icons by Font Awesome and Font Awesome More