DEV Community

Siddharth Shanker
Siddharth Shanker

Posted on

Strings in Python

  • 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."""
Enter fullscreen mode Exit fullscreen mode

String Methods of Python

1. len() :-

This method returns the length of a string.

>>> len(string1)
13
Enter fullscreen mode Exit fullscreen mode

2. .upper():-

  • This method returns a new string with all characters in uppercase.
>>> string2
'This is a string.'
>>> string2.upper()
'THIS IS A STRING.'
Enter fullscreen mode Exit fullscreen mode

3. .lower() :-

  • This method returns a new string with all characters in lowercase.
>>> string2
'This is a string.'
>>> string2.lower()
'this is a string.'
Enter fullscreen mode Exit fullscreen mode

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.'
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

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']
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

8. .strip() :-

This method returns the string with both leading and trailing characters.

>>> string1 = '   this is   a string  '
>>> string1.strip()
'this is   a string'
Enter fullscreen mode Exit fullscreen mode

9. .endswith() :-

This method returns true if the string ends with the specified value.

>>> string1.endswith('aaaaa')
True
>>> string1.endswith('aaaaaa')
False
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

18. .isupper() :-

  • This method returns True if all characters in the string are alphanumeric
>>> string1 = "abac"
>>> string1.isupper()
False
>>> string1 = "ABCA"
>>> string1.isupper()
True
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

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!'
Enter fullscreen mode Exit fullscreen mode

References

Python docs official
W3Schools

Top comments (0)