Strings can be defined as a contiguous sequence of symbols or characters.
In Python Strings are denoted by single, double quotes.
print ('spam eggs')# single quotesprint ('doesn\'t')# use \' to escape the single quote...print ("doesn't")# ...or use double quotes insteadprint ('"Yes," they said.')print ("\"Yes,\" they said.")print ('"Isn\'t," they said.')
spam eggs
doesn't
doesn't
"Yes," they said.
"Yes," they said.
"Isn't," they said.
In the above example, backslash (\) is used as an escape sequence. An escape sequence is nothing but a special character that has a specific function. As shown above, backslash (\) is used to escape the quote.
Creating and Initialising Strings
We can inialise a string in the following ways:
Literal/Constant
Using input() method to take input from user
Indexing
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:
Indices may also be negative numbers, to start counting from the right:
Python Strings are Immutable
Python strings cannot be changed — they are immutable. Therefore, assigning to an indexed position in the string results in an error:
Traversing a string
Traversing a string means accessing all the elements of the string one after the other by using the subscript. A string can be traversed using a 'for' loop or a 'while' loop.
Operations on String
Concatenation (+)
Strings can be concatenated (glued together) with the + operator. Both the operands should be of string datatype else it results in an error
Repetition (*)
Strings can be repeated with the * operator where one operand is the string and the other operand is an integer:
Membership ([not] in)
The in operator returns the boolean value True if the string contains the given character or the sequence of characters and vice-versa for the case of not in operator.
Slicing (str[m:n])
Slicing allows you to obtain a substring
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.
String methods & built in functions
len()
Returns the length of the string:
string.capitalize(word)
Return a copy of word with only its first character capitalized.
string.find(s, sub[, start[, end]])
Return the lowest index in s where the substring sub is found such that sub is wholly contained in s[start:end]. Return -1 on failure. Defaults for start and end and interpretation of negative values is the same as for slices.
str.isalnum()
Return true if all characters in the string are alphanumeric and there is at least one character, false otherwise.
str.isalpha()
Return true if all characters in the string are alphabetic and there is at least one character, false otherwise.
str.isdigit()
Return true if all characters in the string are digits and there is at least one character, false otherwise.
str.lower()
Returns the exact copy of the string with all the letters in lowercase.
str.islower()
Returns True if the string is in lowercase else returns False.
str.isupper()
Returns True if the string is in uppercase.
str.upper()
Returns the exact copy of the string with all letters in uppercase.
str.lstrip([chars])
Return a copy of the string with leading characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix; rather, all combinations of its values are stripped
str.rstrip([chars])
Return a copy of the string with trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a suffix; rather, all combinations of its values are stripped:
str.isspace()
Return true if there are only whitespace characters in the string and there is at least one character, false otherwise.
str.istitle()
Return true if the string is a titlecased string and there is at least one character, for example uppercase characters may only follow uncased characters and lowercase characters only cased ones. Return false otherwise.
str.replace(old, new[, count])
Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.
str.join(iterable)
Returns a string in which the string elements have been joined by a separator.
str.swapcase()
Return a copy of the string with uppercase characters converted to lowercase and vice versa.
str.partition(sep)
The function partitions the strings at the first occurrence of separator, and returns the strings partition in three parts i.e. before the separator, the separator itself, and the part after the separator. If the separator is not found, returns the string itself, followed by two empty strings.
str.split([sep[, maxsplit]])
The function splits the string into substrings using the separator. The second argument is optional and its default value is zero. If an integer value N is given for the second argument, the string is split in N+1 strings.
strLiteral = 'spam eggs'
print(strLiteral)
strInput = input()
print (strInput) #hi gets printed only once below, the first hi is the input statement.
spam eggs
hi
hi
word = 'Python'
print (word[0]) # character in position 0
print (word[5]) # character in position 5
P
n
word = 'Python'
print (word[-1]) # last character
print (word[-2]) # second-last character
print (word[-6])
n
o
P
word = 'Python'
word[0] = 'J'
TypeError Traceback (most recent call last)
<ipython-input-26-ad89e228b316> in <module>
1 word = 'Python'
----> 2 word[0] = 'J'
TypeError: 'str' object does not support item assignment
word = 'Python'
# Traversal using for loop
for i in word:
print(i)
print ('')
# Traversal using while loop
i = 0
while i < len(word):
print( word[i])
i = i + 1
P
y
t
h
o
n
P
y
t
h
o
n
# 'un', followed by 'ium'
word = 'un' + 'ium'
print( word)
# Concatenating an integer and a string leads to an error
word = 'un' + 3
print( word)
unium
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-28-242014416292> in <module>
4
5 # Concatenating an integer and a string leads to an error
----> 6 word = 'un' + 3
7 print( word)
TypeError: can only concatenate str (not "int") to str
# 3 times 'Python'
word = 3 * 'Python'
print( word)
PythonPythonPython
word = 'crossing farewell'
# Usage of in
evalIn = 'fare' in word
print( evalIn)
# Usage of not in
evalNotIn = 'fare' not in word
print(evalNotIn)
True
False
word = 'Python'
print (word[0:2]) # characters from position 0 (included) to 2 (excluded)
print (word[2:5]) # characters from position 2 (included) to 5 (excluded)
print (word[:2]) # character from the beginning to position 2 (excluded)
print (word[4:]) # characters from position 4 (included) to the end
print (word[-2:]) # characters from the second-last (included) to the end
Py
tho
Py
on
on
word = 'Python'
print (len(word))
6
word = 'liGHT battlefield'
print (word.capitalize())
Light battlefield
word = 'mammals'
# On omitting the start parameters, the function starts the search from the
# beginning
print (word.find('ma'))
print (word.find('ma',2))
# Displays -1 because the substring could not be found between the index 2
# and 4-1
print (word.find('ma',2,4))
print (word.find('ma',2,5))
0
3
-1
3
word = 'institution kitchen'
print (word.isalnum()) # Returns False as space is not an alphanumeric character
word = 'institution4kitchen'
print (word.isalnum()) # Returns True
False
True
word = 'apparatus123'
print (word.isalpha()) # Returns False as it contains numeric values
word = 'apparatus'
print (word.isalpha()) # Returns True
False
True
word = 'displace123'
print (word.isdigit())
word = '123'
print (word.isdigit())
False
True
word = 'fUnCtIonAl jUnGlE'
print (word.lower())
functional jungle
word = 'Python'
print (word.islower())
word = 'python'
print (word.islower())
False
True
word = 'PYTHON'
print (word.isupper())
word = 'python'
print (word.isupper())
True
False
word = 'fUnCtIonAl jUnGlE'
print (word.upper())
FUNCTIONAL JUNGLE
word = ' spacious '
print (word.lstrip()) # repr() is a method which adds apostrophes
word = 'www.example.com'
print (word.lstrip('www.'))# repr() is a method which adds apostrophes
spacious
example.com
word = ' spacious '
print (repr(word.rstrip()))
word = 'mississippi'
print (repr(word.rstrip('ippi')))
' spacious'
'mississ'
word = ' '
print (word.isspace())
word = 'Python'
print (word.isspace())
True
False
word = 'marsh exact'
print (word.istitle())
word = 'Marsh Exact'
print (word.istitle())
False
True
word = 'mourning silver mourning silver silver mourning silver'
print (word.replace('mourning', 'happy')) # Replaces all occurrences of mourning
print (word.replace('mourning', 'happy', 2)) # Replaces only 2 occurence of mourning