Error:

Create new string

# literal
s = 'test'
# constructor
s = str('test')

Check if the string is equal to another string

s1 = 'test'
s2 = 'Test'
# case sensitive
s1 == s2 # False
# case insensitive
s1.lower() == s2.lower() # True

Compare two strings

s1 = 'abc'
s2 = 'abd'
s1 < s2   # True
s1 <= s2  # True
s1 > s2   # False
s1 >= s2  # False

Get character at index

s = 'abcdefg'
s[3] # 'd'

Get length of the string

s = 'abcdefg'
len(s) # 7

Get substring

s = 'abcdefgh'
s[1:3] # 'bc'
s[-3:] # 'fgh'
s[:3]  # 'abc'
s[1:-1] # 'bcdefg'

Split string using delimiter

'a/b/c/d'.split('/') # ['a', 'b', 'c', 'd']

Split string to characters

list('abcd') # ['a', 'b', 'c', 'd']

Remove part of the string

s = '123abc123'
s = s[:3] + s[6:] # '123123'

Replace part of the string

s = '123abc123'
replacement = 'test'
s = s[:3] + replacement + s[6:] # '123test123'

Replace all occurences of the substring

s = 'foo bar foo'
s1 = s.replace('foo', '[]', 1) # '[] bar foo'
s2 = s.replace('foo', '[]') # '[] bar []'

Remove all occurences of the substring

s = 'foo bar foo'
s = s.replace('foo', '') # ' bar '

Remove all occurences of character set

import re
s = 'foo bar foo'
s = re.sub('[aouie]', '', s) # 'f br f'

Convert char to ASCII code

ch = ord('A') # 65

Convert ASCII code to char

c = chr(65) # 'A'

Process string one character at a time

for ch in 'test':
    print ch
    # or do something useful here

Reverse string by character

s = 'foo bar foo'
s = s[::-1] # 'oof rab oof'

Reverse string by word

s = 'foo bar baz'
words = s.split()
words.reverse()
s = ' '.join(words) # 'baz bar foo'

Expand variable in string

start = 0
step = 10
s = 'Start at %d with step %d' % (start, step)
s # 'Start at 0 with step 10'

Uppercase string

'Test'.upper() # 'TEST'

Lowercase string

'Test'.lower() # 'test'

Capitalize all words

'one and two'.title() # 'One And Two'

Capitalize first word

'one two'.capitalize() # 'One two'

Escape characters in string

import re
s = "It's just an \"example\": \\"
escaped1 = re.escape(s) # 'It\\\'s\\ just\\ an\\ \\"example\\"\\:\\ \\\\'
# or 
escaped2 = s.encode('string-escape') # 'It\\\'s just an "example": \\\\'

Unescape characters in string

s = '\\"Test \\\\ \\"'
s = s.decode('string-escape') #  '"Test \\ "'

URL-encode the string

import urllib
s = urllib.quote_plus('%q1 AND q2%') # '%25q1+AND+q2%25'

URL-decode the string

import urllib
s = urllib.unquote_plus('%25q1+AND+q2%25') # '%q1 AND q2%'

Trim blanks from the ends of a string

s = " \t just an example \r\n"
s1 = s.lstrip() # 'just an example \r\n'
s2 = s.rstrip() # ' \t just an example'
s = s.strip() # 'just an example'

Trim newlines from the ends of a string

s1 = 'test\r\n'
s2 = 'test\n'

s1 = s1.rstrip('\r\n') # 'test'
s2 = s2.rstrip('\r\n') # 'test'

Find position of the substring

s = 'foo bar foo'
pos = s.index('bar') # 4

Check if the string contains another string

s = 'foo bar foo'
if 'bar' in s:
    print "Bar is there"

Check if the string starts with another string

s = 'foo bar foo'
s.startswith('foo') # True

Check if the string ends with another string

s = 'foo bar foo'
s.endswith('foo') # True

Convert the string to integer

s = '123'
i = int(s) # 123

Convert the string to float

s = '123.5'
f = float(s) # 123.5

Convert float value to string

f = 123.5
s = str(f) # '123.5'

Convert integer value to string

f = 123
s = str(f) # '123'

Enumerate all words in the string

s = 'foo bar \t\tbaz'
for word in s.split():
    print word