DEV Community

NK1FC
NK1FC

Posted on

Python String Methods

1- capitalize()

Capitalizes the first letter of a string.

string = 'what is your name?'
capitalized = string.capitalize()
print(capitalized) # 'What is your name?'
Enter fullscreen mode Exit fullscreen mode

2- lower()

Changes all characters in a string to lowercase.

string = 'What Is Your Name?'
lowered = string.lower()
print(lowered) # 'what is your name?'
Enter fullscreen mode Exit fullscreen mode

3- upper()

Changes all characters in a string to uppercase.

string = 'What Is Your Name?'
uppered = string.upper()
print(uppered) # 'WHAT IS YOUR NAME?'
Enter fullscreen mode Exit fullscreen mode

4- swapcase()

Interchange the cases of all characters in a string.

string = 'What Is Your Name?'
swapcased = string.swapcase()
print(swapcased) # 'WHAT IS YOUR NAME?'
Enter fullscreen mode Exit fullscreen mode

5- title()

Capitalizes the first letter of each word in a string.

string = 'what is your name?'
titled = string.title()
print(titled) # 'What Is Your Name?'
Enter fullscreen mode Exit fullscreen mode

6- strip()

Removes whitespace from the start and end of a string.

string = '  what is your name?  '
stripped = string.strip()
print(stripped) # 'what is your name?'
Enter fullscreen mode Exit fullscreen mode

7- count()

Gives the number of occurrences of a substring in a string.

string = 'what is your name?'
counted = string.count('a')
print(counted) # 2
Enter fullscreen mode Exit fullscreen mode

8- find()

Gives the index of the first occurrence of a substring in a string, or -1 if it is not present.

string = 'what is your name?'
found = string.find('is')
print(found) # 5
Enter fullscreen mode Exit fullscreen mode

9- index()

Gives the index of the first occurrence of a substring in a string, or raises a ValueError if it is not present.

string = 'what is your name?'
indexed = string.index('your')
print(indexed) # 9
Enter fullscreen mode Exit fullscreen mode

10- replace()

Interchange all occurrences of a substring in a string with another substring.

string = 'what is your name?'
replaced = string.replace('your', 'my')
print(replaced) # 'what is my name?'
Enter fullscreen mode Exit fullscreen mode

11- format()

Formats a string using placeholders for variables or values.

string = 'what is your name?'
replaced = string.replace('your', 'my')
print(replaced) # 'what is my name?'
Enter fullscreen mode Exit fullscreen mode

12- split()

Splits a string into a list of substrings using a specified delimiter.

formatted = 'My name is {name} and I am {age} years old'.format(name='NK', age=23)
print(formatted) # 'My name is Jane and I am 30 years old'
Enter fullscreen mode Exit fullscreen mode

13- join()

Joins a list of strings into a single string using a specified character or symbols.

string = 'what is your name?'
splitted = string.split()
print(splitted) # ['what', 'is', 'your', 'name?']
Enter fullscreen mode Exit fullscreen mode

14- startswith()

Gives True if a string starts with a specified substring, otherwise False.

joined = '-'.join(['what', 'is', 'your', 'name?'])
print(joined) # 'what-is-your-name?'
Enter fullscreen mode Exit fullscreen mode

15- endswith()

Gives True if a string ends with a specified substring, otherwise False.

string = 'what is your name?'
startswith = string.startswith('what')
print(startswith) # True
Enter fullscreen mode Exit fullscreen mode

Top comments (0)