Icon
Login    Register          




Most Advanced AI

Generate Code at the speed of Thought

Improve Productivity of Developers



Python Tutorial

Python String Methods

Python String Methods are built-in functions that allow us to manipulate strings. They are used to perform various operations on strings such as searching, replacing, splitting, and formatting.

1. capitalize()

This method capitalizes the first letter of a string.

Example:

str = "python programming"
print(str.capitalize())

Output: Python programming

2. count()

This method returns the number of times a specified value appears in a string.

Example:

str = "Python is a programming language"

print(str.count("a"))

Output: 1

3. endswith()

This method returns True if a string ends with the specified suffix, otherwise it returns False.

Example:

str = "Python is a programming language"
print(str.endswith("language"))

Output: True

4. find()

This method returns the index of the first occurrence of the specified value in a string.

Example:

str = "Python is a programming language"
print(str.find("a"))

Output: 11

5. format()

This method formats a specified string into a specified format.

Example:

str = "My name is {name}"
print(str.format(name="John"))

Output: My name is John

6. index()

This method returns the index of the first occurrence of the specified value in a string.

Example:

str = "Python is a programming language"
print(str.index("a"))

Output: 11

7. isalnum()

This method returns True if all characters in a string are alphanumeric, otherwise it returns False.

Example:

str = "Python123"
print(str.isalnum())

Output: True

8. isalpha()

This method returns True if all characters in a string are alphabetic, otherwise it returns False.

Example:

str = "Python"

print(str.isalpha())

Output: True

9. isdigit()

This method returns True if all characters in a string are digits, otherwise it returns False.

Example:

str = "12345"
print(str.isdigit())

Output: True

10. join()

This method joins all elements of an iterable to a string.

Example:

str = "-"
list = ["Python", "Programming", "Language"]
print(str.join(list))

Output: Python-Programming-Language


Search Menu



Disclaimer:
Patodia Infotech Private Limited (fastdevai.com) has no responsibility for any text or code generated or for any action taken by you based on the contents provided by this web site. The use of this web site is solely at the user's risk. All the services are provided with the understanding that Patodia Infotech Private Limited or any other person or company associated with this company or website are not responsible for any loss or damage occurring to anybody from any code or contents generated by this website. Please note that text, codes and contents are generated by AI. These are state-of-the-art AI solutions. However, these are AI tools so there is no guarantee of accuracy of any code or contents generated.


Copyright © 2021-2024 Patodia Infotech Private Limited, All Rights Reserved.