Strings

Strings are amongst the most popular types in Python. They can be enclosed in single quotes (‘…’) or double quotes (“…”) with the same result.

Creating strings is as simple as assigning a value to a variable. For example −

string1 = "Hello World"

string2 = "My name is John Doe"

Please note that string in single quotes cannot hold any other single quoted character in it otherwise an error arises. To overcome this error, use of double quotes is preferred, because it helps in creation of Strings with single quotes in them. Fo example in the python interpreter:

>>> print "It doesn't help"

It doesn't help

>>> print 'It doesn't help'

  File "<stdin>", line 1

    print 'It doesn't help'

                    ^

SyntaxError: invalid syntax

Alternatively if using single quotes, we use a slash \  to escape quotes:

>>> print 'It doesn\'t help'

It doesn't help

If you don’t want characters prefaced by \ to be interpreted as special characters, you can use raw strings by adding an r before the first quote:

>>> print('C:\some\name'# here \n means newline!

C:\some

ame

>>> print(r'C:\some\name'# note the r before the quote

C:\some\name

 

String literals can span multiple lines. One way is using triple-quotes: “””…””” or ”’…”’. End of lines are automatically included in the string, but it’s possible to prevent this by adding a \ at the end of the line. The following example:

print("""\

Usage: thingy [OPTIONS]

     -h                        Display this usage message

     -H hostname               Hostname to connect to

""")

Output

Usage: thingy [OPTIONS]

     -h                        Display this usage message

     -H hostname               Hostname to connect to

Note the initial new line is not included.

Strings can be concatenated (glued together) with the + operator, and repeated with * eg

# Print ‘py 4 times followed by ‘thon’

>>> 'py'* 4 + 'thon'

'pypypypython'

 

Accessing characters in Python

Strings can be indexed (subscripted), with the first character having index 0. There is no separate character type; a character is simply a string of size one:

>>> word = 'Python'

>>> word[0]  # character in position 0

'P'

>>> word[3]  # character in position 5

'h'

Indices may also be negative numbers, to start counting from the right:

>>> word[-1]  # last character

'n'

>>> word[-2]  # second-last character

'o'

>>> word[-6]

'P'

Note that since -0 is the same as 0, negative indices start from -1.

To access substrings, use square brackets for slicing along with the index or indices to obtain your substring. For example

>>> word[0:2# characters from position 0 (included) to 2 (excluded)

'Py'

>>> word[2:5# characters from position 2 (included) to 5 (excluded)

'tho'

Note how the start is always included, and the end always excluded. This makes sure that s[:i] + s[i:] is always equal to s:

>>> word[:2] + word[2:]

'Python'

>>> word[:4] + word[4:]

'Python'

Slice indices have useful defaults; an omitted first index defaults to zero, an omitted second index defaults to the size of the string being sliced.

>>> word[:2]   # character from the beginning to position 2 (excluded)

'Py'

>>> word[4:]   # characters from position 4 (included) to the end

'on'

>>> word[-2:]  # characters from the second-last (included) to the end

'on'

One way to remember how slices work is to think of the indices as pointing between characters, with the left edge of the first character numbered 0. Then the right edge of the last character of a string of n characters has index n, for example:

+---+---+---+---+---+---+

 | P | y | t | h | o | n |

 +---+---+---+---+---+---+

 0   1   2   3   4   5   6

-6  -5  -4  -3  -2  -1

Python strings cannot be changed — they are immutable. Therefore, assigning to an indexed position in the string results in an error:

>>> word[2:] = 'py'

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: 'str' object does not support item assignment

If you need a different string, you should create a new one:

>>> word[:2] + 'py'

'Pypy'

The built-in function len() returns the length of a string:

>>> s = 'supercalifragilisticexpialidocious'

>>> len(s)

34

Built-in string methods

Here are some common built-in string functions used to manipulate strings. For more checkout the python doc

str.capitalize()

Capitalizes first letter of string

str.count(str, beg= 0,end=len(string))

Counts how many times str occurs in string or in a substring of string if starting index beg and ending index end are given.

str.endswith(suffix, beg=0, end=len(string))

Determines if string or a substring of string (if starting index beg and ending index end are given) ends with suffix; returns true if so and false otherwise.

str.find(str, beg=0 end=len(string))

Determine if str occurs in string or in a substring of string if starting index beg and ending index end are given returns index if found and -1 otherwise.

str.index(str, beg=0, end=len(string))

Same as find(), but raises an exception if str not found.

str.isalnum()

Returns true if string has at least 1 character and all characters are alphanumeric and false otherwise.

str.isalpha()

Returns true if string has at least 1 character and all characters are alphabetic and false otherwise.

str.isdigit()

Returns true if string contains only digits and false otherwise.

 

str.islower()

Returns true if string has at least 1 cased character and all cased characters are in lowercase and false otherwise.

 

str.isnumeric()

Returns true if a unicode string contains only numeric characters and false otherwise.

 

str.isspace()

Returns true if string contains only whitespace characters and false otherwise.

str.istitle()

Returns true if string is properly “titlecased” and false otherwise.

str.isupper()

Returns true if string has at least one cased character and all cased characters are in uppercase and false otherwise.

join(seq)

Merges (concatenates) the string representations of elements in sequence seq into a string, with separator string. For Example

>>> seq = ['I', 'love', 'Python']

>>> '  '.join(seq)

'I love Python'

len(string)

Returns the length of the string

str.lower()

Converts all uppercase letters in string to lowercase.

lstrip()

Removes all leading whitespace in string.

max(str)

Returns the max alphabetical character from the string str.

min(str)

Returns the min alphabetical character from the string str.

str.replace(old, new [, max])

Replaces all occurrences of old in string with new or at most max occurrences if max given.

str.split(sep=None, maxsplit=-1)

Return a list of the words in the string, using sep as the delimiter string. For example

>>> '1,2,3'.split(',')

['1', '2', '3']

>>> '1,2,3'.split(',', maxsplit=1)

['1', '2,3']

>>> '1,2,,3,'.split(',')

['1', '2', '', '3', '']

str.startswith(str, beg=0,end=len(string))

Determines if string or a substring of string (if starting index beg and ending index end are given) starts with substring str; returns true if so and false otherwise.

strip([chars])

Return a copy of the string with the leading and trailing characters removed.

>>> '   spacious   '.strip()

'spacious'

>>> 'www.example.com'.strip('cmowz.')

'example'

 

str.upper()

Converts lowercase letters in string to uppercase.