String
Strings can be created by enclosing characters inside a single quote or double quotes. Even triple quotes can be used in Python but generally used to represent multiline strings and doc strings.
Convert from other type with str()
Slicing: similar as in list
Reverse
Note that reverse() doesn't work for string. Need to convert string to list and then reverse().
Important: We can't do slicing and reverse simultaneously. If we want to reverse a substring:
Change a string
Strings are immutable. This means that elements of a string cannot be changed once it has been assigned. We can simply reassign different strings to the same name.
Delete characters from a string
We cannot delete or remove characters from a string. But deleting the string entirely is possible using the keyword
del.
Concatenation of Two or More Strings
Joining of two or more strings into a single one is called concatenation.
The +
operator does this in Python. Simply writing two string literals together also concatenates them.
The *
operator can be used to repeat the string for a given number of times.
String Membership Test
We can test if a sub string exists within a string or not, using the keywordin
.
String Comparison
We can use == to compare if two strings are the same.
Iterate each character in a string
Convert all characters to uppercase/lowercase
These make a new string with all letters converted to uppercase and lowercase, respectively.
startswith/endswith
This is used to determine whether the string starts with something or ends with something, respectively.
Split
This splits the string into a bunch of strings grouped together in a list. If not specified, the default of string.split() is splitting by space.
Enumerate
The enumerate()
function returns an enumerate object. It contains the index and value of all the items in the string as pairs. This can be useful for iteration.
Join strings
Length
Length including punctuation and spaces.
Count
differentiate uppercase and lowercase
can count the number of space
can count the number of punctuation
Finding substring
Both index
and find
return the index of the first occurrence of the first character in the substring. The only difference is that when the substring is not found, index
returns ValueError
and find
returns -1
.
Replace part of a string
When using replace() to replace part of a string, need to assign the new value to the original name. Because string is immutable. Otherwise, the string won't be changed.
Check if a string is letter
str.isalpha()
Return true if all characters in the string are alphabetic and there is at least one character, false otherwise.
A list of build-in functions for strings
Method
Description
Converts first character to Capital Letter
Pads string with specified character
converts to casefolded strings
returns occurrences of substring in string
Checks if String Ends with the Specified Suffix
Replaces Tab character With Spaces
returns encoded string of given string
Returns the Highest Index of Substring
formats string into nicer output
Returns Index of Substring
Checks Alphanumeric Character
Checks if All Characters are Alphabets
Checks Decimal Characters
Checks Digit Characters
Checks for Valid Identifier
Checks if all Alphabets in a String are Lowercase
Checks Numeric Characters
Checks Printable Character
Checks Whitespace Characters
Checks for Titlecased String
returns if all characters are uppercase characters
Returns a Concatenated String
returns left-justified string of given width
returns right-justified string of given width
returns lowercased string
returns uppercased string
swap uppercase characters to lowercase; vice versa
Removes Leading Characters
Removes Trailing Characters
Removes Both Leading and Trailing Characters
Returns a Tuple
returns a translation table
Returns a Tuple
returns mapped charactered string
Replaces Substring Inside
Returns the Highest Index of Substring
Returns Highest Index of Substring
Splits String from Left
Splits String From Right
Splits String at Line Boundaries
Checks if String Starts with the Specified String
Returns a Title Cased String
Returns a Copy of The String Padded With Zeros
Formats the String Using Dictionary
Checks if any Element of an Iterable is True
returns true when all elements in iterable is true
Returns String Containing Printable Representation
Coverts a Value to Boolean
returns array of given byte size
returns immutable bytes object
Returns a Python code object
Creates a Complex Number
Returns an Enumerate Object
constructs iterator from elements which are true
returns floating point number from number, string
reads and returns a line of string
returns integer from a number or string
returns iterator for an object
Returns Length of an Object
returns largest element
returns smallest element
Applies Function and Returns a List
returns Unicode code point for Unicode character
returns reversed iterator of a sequence
creates a slice object specified by range()
returns sorted list from a given iterable
Add items of an Iterable
Returns an Iterator of Tuples
Last updated