- In Python, a string is a sequence of characters enclosed in quotes. Strings can be created using single quotes (
'), double quotes ("), or triple quotes ('''or""") depending on the requirements. - Strings in Python are immutable, meaning once they are created, their contents cannot be changed.
- However, you can create a new string by concatenating existing strings or by using string methods.
>>> string1 = 'Hello, World!'
>>> string2 = "This is a string."
>>> string3 = '''This is a
... multi-line string.'''
>>> string4 = """This is also
... a multi-line string."""
String Methods of Python
1. len() :-
This method returns the length of a string.
>>> len(string1)
13
2. .upper():-
- This method returns a new string with all characters in uppercase.
>>> string2
'This is a string.'
>>> string2.upper()
'THIS IS A STRING.'
3. .lower() :-
- This method returns a new string with all characters in lowercase.
>>> string2
'This is a string.'
>>> string2.lower()
'this is a string.'
4. .capitalize() :-
This method returns a new string with the first character capitalized.
>>> string4 = string2.lower()
>>> string4
'this is a string.'
>>> string4.capitalize()
'This is a string.'
>>> string4
'this is a string.'
5. .title():-
This method returns a new string with the first character of each word capitalized.
>>> string3 = "This is a small string"
>>> string3.title()
'This Is A Small String'
6. split(delimiter) :-
This method returns a list of strings by splitting the original string at each occurrence of a specified delimiter.
By default, delimiter is space
example:-
>>> string3
'This is a small string'
>>> string3.split()
['This', 'is', 'a', 'small', 'string']
>>> string3 = 'This_is_a_string'
>>> string3.split('_')
['This', 'is', 'a', 'string']
7. count(x) :-
The .count() method returns the number of times an item appears in the string.
>>> string1 = 'Lets count a many times aaaaa'
>>> string1.count('a')
7
8. .strip() :-
This method returns the string with both leading and trailing characters.
>>> string1 = ' this is a string '
>>> string1.strip()
'this is a string'
9. .endswith() :-
This method returns true if the string ends with the specified value.
>>> string1.endswith('aaaaa')
True
>>> string1.endswith('aaaaaa')
False
10. .find() :-
- This method searches the string for a specified value and returns the position of where it was found.
>>> string1
'Lets count a many times aaaaa'
>>> string1.find('m')
13
>>> string1.find('tim')
18
11. .startswith() :-
- This method returns true if the string starts with the specified value
>>> string1
'Lets count a many times aaaaa'
>>> string1.startswith('L')
True
>>> string1.startswith('l')
False
12. .swapcase() :-
- This method swaps cases, lower case becomes upper case and vice versa
>>> string1 = "LETS SEE swap of casing"
>>> string1.swapcase()
'lets see SWAP OF CASING'
13. .isalnum() :-
- This method returns True if all characters in the string are alphanumeric
>>> string1 = "abc 89a"
>>> string1.isalnum()
False
>>> string1 = "abc89a"
>>> string1.isalnum()
True
14. .isalpha() :-
- This method returns True if all characters in the string are in the alphabet
>>> string1
'abc89a'
>>> string1.isalpha()
False
>>> string1='abcd'
>>> string1.isalpha()
True
15. .isnumeric() :-
- This method returns True if all characters in the string are numeric
>>> string1 = "abc89a"
>>> string1.isnumeric()
False
>>> string1 = "89.456"
>>> string1.isnumeric()
True
16. .isdigit() :-
- This method returns true if all characters in the string are digits.
>>> string1 = "abc 89a"
>>> string1.isdigit()
False
>>> string1 = "89"
>>> string1.isalnum()
True
17. .islower() :-
- This method returns True if all characters in the string are in lowercase.
>>> string1 = "abAde"
>>> string1.islower()
False
>>> string1 = "abcdae"
>>> string1.islower()
True
18. .isupper() :-
- This method returns True if all characters in the string are alphanumeric
>>> string1 = "abac"
>>> string1.isupper()
False
>>> string1 = "ABCA"
>>> string1.isupper()
True
19. .replace() :-
- This method returns a string where a specified value is replaced with a specified value
>>> string1='abcd'
>>> string2 = 'efg'
>>> string1.replace('d' , string2)
'abcefg'
20. .format() :-
- This method formats specified values in a string
>>> txt = "For only {price:.2f} dollars!"
>>> txt.format(price = 49)
'For only 49.00 dollars!'
Top comments (0)