Strings
Strings can be defined as a contiguous sequence of symbols or characters.
In Python Strings are denoted by single, double quotes.
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.
Last updated