DEV Community

Shubham Kumar Gupta
Shubham Kumar Gupta

Posted on

String Methods in Python

String Methods in Python

1. upper():

Returns a copy of string in upper-case.

syntax: str.upper()

string = 'My name is Shubham.'

print(string.upper())
Enter fullscreen mode Exit fullscreen mode

output: MY NAME IS SHUBHAM.

2. lower():

Returns a copy of string in lower-case.

syntax: str.lower()

string = 'My name is Shubham.'

print(string.lower())
Enter fullscreen mode Exit fullscreen mode

output: 'my name is shubham.'

3. swapcase()

Returns a copy of string in swapped-case.

syntax: str.swapcase()

string = 'My name is Shubham.'

ptint(string.swapcase())
Enter fullscreen mode Exit fullscreen mode

output: 'mY NAME IS sHUBHAM.'

4. capitalize()

Returns a copy of string with first letter in upper-case and rest in lower-case.

syntax: str.capitalize()

string = 'My name is Shubham.'

print(string.capitalize())
Enter fullscreen mode Exit fullscreen mode

output: My name is shubham.

5. title()

Returns a copy of string in title-case.

syntax: str.title()

string = 'My name is Shubham.'

print(string.title())
Enter fullscreen mode Exit fullscreen mode

output: My Name Is Shubham.

6. isupper()

Returns boolean value if all the letters in string are upper-case.

syntax: str.isupper()

string1 = 'MY NAME IS SHUBHAM.'
string2 = 'My name is Shubham.'

print(string1.isupper())
print(string2.isupper())
Enter fullscreen mode Exit fullscreen mode

output: True, False

7. islower()

Returns boolean value if all the letters in string are lower-case.

syntax: strislower()

string1 = 'my name is shubham.'
string2 = 'My name is Shubham.'

print(string1.islower())
print(string2.islower())
Enter fullscreen mode Exit fullscreen mode

output: True, False

8. isalnum():

Returns boolean value if all the characters in the string are alpha-numeric.

syntax: str.isalnum()

string1 = 'My1name2is3Shubham4'
string2 = 'My name is Shubham.'

print(string1.isalnum())
print(string2.isalnum())
Enter fullscreen mode Exit fullscreen mode

output: True, False

9. isalpha():

Returns boolean value if all the characters in the string are alphabet.

syntax: str.isalpha()

string1 = 'MyNameIsShubham'
string2 = 'My name is Shubham.'

print(string1.isalnum())
print(string2.isalnum())
Enter fullscreen mode Exit fullscreen mode

output: True, False

10. isdigit():

Returns boolean value if all the characters in the string are digit.

syntax: str.isdigit()

string1 = '1234'
string2 = 'My name is Shubham.'

print(string1.isdigit())
print(string2.isdigit())
Enter fullscreen mode Exit fullscreen mode

output: True, False

11. startswith(str):

Returns boolean if the string starts with given sub-string.

syntax: str.startswith(sub_string)

string = 'My name is Shubham.'

print(string.startswith('My'))
print(string.startswith('Shubham.'))
Enter fullscreen mode Exit fullscreen mode

output: True, False

12. endswith(str):

Returns boolean if the string ends with given sub-string.

syntax: str.endswith(sub_string)

string = 'My name is Shubham.'

print(string.endswith('Shubham.'))
print(string.endswith('My'))
Enter fullscreen mode Exit fullscreen mode

output: True, False

13. count(str):

Returns the count of the number of times a substring appears in the string.

syntax: str.count(sub_str)

string = 'My name is Shubham and my favorite coding language is Python.'

print(string.count('is'))
Enter fullscreen mode Exit fullscreen mode

output: 2

14. find(str):

Returns the index of first occurance of given substring in string. Return -1 if given substring is not present in string.

syntax: str.found(sub_str)

string = 'My name is Shubham.'

print(string.find('name'))
print(string.find('years'))
Enter fullscreen mode Exit fullscreen mode

output: 3, -1

15. replace(str1, str2):

Returns a string replacing substring in the first parameter from the string with substring in the second parameter.

syntax: str.replace(sub_str1, sub_str2)

string = 'My name is Shubham.'

print(string.replace('My name is', 'Myself'))
Enter fullscreen mode Exit fullscreen mode

output: Myself Shubham.

16. split():

Returns a list of words splitted from the given string.

syntax: str.split()

string = 'My name is Shubham.'

print(string.split())
Enter fullscreen mode Exit fullscreen mode

output: ['My', 'name', 'is', 'Shubham.']

17. join(itr):

Returns a string joined from the elements of given iterable.

syntax: "*".join(iterable)

l = ['My', 'name', 'is', 'Shubham.']

print(" ".join(l))
Enter fullscreen mode Exit fullscreen mode

output: My name is Shubham.

REFERENCES

Top comments (0)