String Methods
Roshansahurk
Posted on February 16, 2023
What are Strings?
Strings are the data structures which store a sequence of characters.
Strings are immutable i.e. they cannot be changed.
Ex: "Hello"
"Hello" is a string of characters with characters 'h', 'e', 'l', 'l', and 'o' in it.
A string can be initialised by single quotes and double quotes. It throws an array if used in combinations of single quotes and double quotes.
-
Initialise a string.
>>> s = 'abcde' >>> s #Prints the output 'abcde'
The indexing on a string starts with 0 from the start and -1 from the end.
-
Accessing a particular index in a string.
>>> s = 'Hello' >>> s[2] l >>> s[-4] e
-
Concatenation in a string.
>>> a = "Python" >>> b = "3.7" >>> a + " " + b "Python 3.7"
Slicing in a string is done with the help of square brackets.
-
s[start:stop:step]
>>> s = 'Pythonist' >>> s[2:4] th
Helper Functions in strings
-
string.capitalize()
Returns a copy of a string with the first letter capitalised.
>>> s.capitalize() 'Abcde'
-
string.lower()
Return a copy of a string with all the characters in lowercase.
>>> a = 'Hello WORLD' >>> a.lower() hello world
-
string.upper()
Return a copy of a string with all the characters in lowercase.
>>> a.upper() HELLO WORLD
-
string.split()
Returns a list with the space-separated elements.
>>> a.split() ["Hello", "World"]
-
string.join()
Returns a string by joining the entire set of characters in a list or a tuple.
>>> l1 = ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd'] >>> ''.join(l1) 'Hello World'
-
string.swapcase()
Returns a copy of string but with lowercase letters to uppercase and vice versa.
>>> a = 'Python' >>> a.swapcase() 'pYTHON'
-
string.casefold()
Return a copy of a string with all the characters in lowercase. It is the same as the string.lower()
>>> a = 'Hello WORLD' >>> a.casefold() hello world
-
string.center(width, fill_char)
Returns a copy of the string in the centre with added spaces to the right and left. It takes space as a fill character if nothing is given.
>>> s = 'HELLO' >>> s.center(30, '-') '------------HELLO-------------'
-
string.count(string)
Returns an integer value stating the number of occurrences of a string in a string.
>>> s = 'HELLO' >>> s.count('L') 2
-
string.endswith(string)
Returns True if the string end with the specified string.
>>> s = 'Python' >>> s.endswith("on") True
-
string.index(string)
Returns the index of the first occurrence in a string.
>>> s = 'Hello World' >>> s.index('o') 4
-
string.isalnum()
Returns True if all characters in the string are alphanumeric.
>>> s = 'AabcD123' >>> s.isalnum() True
-
string.isalpha()
Returns True if all characters in the string are in the alphabet.
>>> s = 'abcd' >>> s.isalpha() True
-
string.isdecimal()
Returns True if all characters in the string are decimals.
>>> s = '1234' >>> s.isdecimal() True
-
string.isdigit()
Returns True if all characters in the string are digits.
>>> s = '1234' >>> s.isdecimal() True
-
string.islower()
Returns True if all characters in the string are lowercase.
>>> s = 'abcd' >>> s.islower() True
-
string.isupper()
Returns True if all characters in the string are uppercase.
>>> s = 'ABCD' >>> s.isupper() True
References
-
To know more about the helper functions in a string, click on the below link.
Posted on February 16, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.