String Manipulation

The following snippet shows the most important built in functions that you should be familiar.


In [69]:
sample_string = "  this sample string will be used as a SAMPLE string "

In [70]:
# Slicing work the same way as in arrays
sample_string[:13]


Out[70]:
'  this sample'

In [71]:
# Splits string according to delimiter and returns list of substrings;
# space is the default delimiter
# split into at most n substrings, where n is optional
sample_string.split(' ', 4)


Out[71]:
['', '', 'this', 'sample', 'string will be used as a SAMPLE string ']

In [72]:
# Splits string at all (or num) NEWLINEs and returns a list of each line with NEWLINEs removed.
sample_string.splitlines()


Out[72]:
['  this sample string will be used as a SAMPLE string ']

In [73]:
# Replaces all occurrences of the in the string of first argument with
# the second one at most n occurrences
sample_string.replace(' ', '$')


Out[73]:
'$$this$sample$string$will$be$used$as$a$SAMPLE$string$'

In [74]:
# Removes all leading and trailing repetition of the provided pattern in string
print "strip:", sample_string.strip('s')
# Removes all leading repetition of the provided pattern in string
print "rstrip:", sample_string.rstrip()
# Removes all  trailing repetition of the provided pattern in string
print "lstrip:", sample_string.lstrip()


strip:   this sample string will be used as a SAMPLE string 
rstrip:   this sample string will be used as a SAMPLE string
lstrip: this sample string will be used as a SAMPLE string 

In [75]:
# Return the string of the object parameter
print "str:", str('123')
# Return the Unicode string of the object parameter
print "unicode", unicode('123')
# Given a string of length one, return an integer representing the Unicode code
print "ord:", ord('a')
# Return a string of one character whose ASCII code is the integer parameter
print "chr", chr(97)
# Return the Unicode string of one character whose Unicode code is the integer i
print "unichr:", unichr(97)


str: 123
unicode 123
ord: 97
chr a
unichr: a

In [76]:
# Converts all uppercase/lowercase letters to lowercase/uppercase
print "   lower:", sample_string.lower()
print "   upper:", sample_string.upper()
# Inverts case for all letters
print "swapcase:", sample_string.swapcase()


   lower:   this sample string will be used as a sample string 
   upper:   THIS SAMPLE STRING WILL BE USED AS A SAMPLE STRING 
swapcase:   THIS SAMPLE STRING WILL BE USED AS A sample STRING 

Secondary methods

Although important, you will probably use them less frequently.


In [77]:
# Capitalizes first letter of string
sample_string.capitalize()


Out[77]:
'  this sample string will be used as a sample string '

In [78]:
# Returns a string center for the width provided.
sample_string.center(80, ' ')


Out[78]:
'               this sample string will be used as a SAMPLE string               '

In [79]:
# Counts how many times str occurs in string or substring if setting min and max indices are set
# IT´S CASE SENSITIVE
print sample_string.count('string') 
print sample_string.count('string', 0, 30)


2
1

In [80]:
# Test if string or a substring of string ends with provided string
print sample_string.endswith('SAMPLE') 
print sample_string.endswith('SAMPLE', 0, len(sample_string)-8)


False
True

In [81]:
# Determine if provided string occurs in string or in a substring
# Return index of first appearance or -1 if not found
print "find:", sample_string.find('string') #(beg=0 end=len(string))
# Performs search starting from the right
print "rfind:", sample_string.rfind('string') #(beg=0 end=len(string))


find: 14
rfind: 46

In [82]:
# Determine if provided string occurs in string or in a substring
# Return index of first appearance or raised an exception if not found
print "index:", sample_string.index('will be') #(beg=0, end=len(string))
# Performs index search starting from the right
print "rindex:", sample_string.rindex('will be') #(beg=0, end=len(string))


index: 21
rindex: 21

In [83]:
# Returns true if string has at least 1 character and all characters
# are alphanumeric and false otherwise.
print "isalnum()", sample_string.isalnum()
# Returns true if string has at least 1 character and all characters
# are alphabetic and false otherwise.
print "isalpha()", sample_string.isalpha()
# Returns true if string contains only digits and false otherwise.
print "isdigit()", sample_string.isdigit()
# Returns true if string has at least 1 cased character and all cased 
# characters are in lowercase and false otherwise.
print "islower()", sample_string.islower()
# Returns true if string contains only whitespace characters and 
# false otherwise.
print "isspace()", sample_string.isspace()
# Returns true if string is properly "titlecased" and false otherwise.
sample_string.istitle()
# Returns true if string has at least one cased character and all 
# cased characters are in uppercase and false otherwise.
print "isupper()", sample_string.isupper()


isalnum() False
isalpha() False
isdigit() False
islower() False
isspace() False
isupper() False

In [84]:
# Returns a string with the original string left-justified 
print "ljust:", sample_string.ljust(80,' ')
# Returns a string with the original string right-justified to a total of width columns
print "rjust:", sample_string.rjust(80,' ')


ljust:   this sample string will be used as a SAMPLE string                            
rjust:                              this sample string will be used as a SAMPLE string