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]:
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]:
In [72]:
# Splits string at all (or num) NEWLINEs and returns a list of each line with NEWLINEs removed.
sample_string.splitlines()
Out[72]:
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]:
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()
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)
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()
Although important, you will probably use them less frequently.
In [77]:
# Capitalizes first letter of string
sample_string.capitalize()
Out[77]:
In [78]:
# Returns a string center for the width provided.
sample_string.center(80, ' ')
Out[78]:
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)
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)
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))
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))
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()
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,' ')
For a more detailed explanation of string refer to:
[1] https://docs.python.org/2/library/string.html
[2] https://www.tutorialspoint.com/python/python_strings.htm