EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Python 3 String Methods

Secondary Sidebar
Python 3 Tutorial
  • Python 3 Tutorial
    • Python 3 Commands
    • Python 3 Array
    • Python 3 Operators
    • Python 3 NumPy
    • Python 3 Webserver
    • Python 3 yield
    • Python 3 Pip
    • Python 3 Install
    • Python 3 raw_input
    • Python 3 HTTP Server
    • Python 3 Threading
    • Python 3 Requests
    • Python 3 Module Index
    • Python 3 String Format
    • Python 3 Unicode
    • Python 3 GUI
    • Python 3 xrange
    • Python 3 Tuple
    • Python 3 input
    • Python 3 JSON
    • Python 3 string
    • Python 3 try-except
    • Python 3 RegEx
    • Python 3 Object-Oriented Programming
    • Python 3 zip
    • Python 3 Exception
    • Python 3 write to file
    • Python 3 Functions
    • Python 3 List Function
    • Python 3 While Loop
    • Python 3 Global Variable
    • Python 3 String Methods
    • Python 3 interpreter
    • Python 3 REPL
    • Python 3 else if
    • Python 3 basics
    • Python 3 cheat sheet
    • Python 3 Print
    • Python 3 For Loop
    • Python 3 range
    • Python 3 Dictionary
    • Python 3 has_key
    • Python 3 URLlib
Home Software Development Software Development Tutorials Python 3 Tutorial Python 3 String Methods

Python 3 String Methods

Definition of Python 3 String Methods

Python 3 string methods are one of Python’s most common data types. Simply wrapping characters in quotations allows us to construct them. Assigning a value to a variable is the same as creating strings. A string in Python is a collection of Unicode characters surrounded by quotation marks. Every string method altered characteristics rather than modifying the original string. There are multiple string methods are available in python.

Introduction to python 3 string methods

  • Python supports a character type, these are handled as one-length strings and thus considered substrings. To get a substring, use square brackets for slicing and the index or indices.
  • A string is a collection of characters surrounded by quotation marks to concatenate two strings, for instance, use the join method. Python 3 string method is very important and useful in python, different methods have a different use.

Python 3 string methods list

  • Below are string methods available in python are as follows. String methods are very important in python.

1. Capitalize

  • Returns a string copy with the first letter capitalized and the remainder lowercased.
  • If we wish all words’ first letters capitalized, use title. The below example shows capitalize string methods in python.

Code:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

py = "python 3 string methods"
print (py.capitalize ())
py = "python 3 string methods"
print (py.title())

Output:

Python 3 String Methods 1

2. Casefold

  • Returns a string that has been case-folded. For caseless matching, casefolded strings can be utilized.
  • The below example shows casefold string methods in python.

Code:

py = "Python String Methods"
py1 = py.casefold()
print(py1)

Output:

Python 3 String Methods 2

3. Center

  • Returns the centered string in a length width string. The provided fill char can be used to add padding. If width equals or is less than len.
  • The below example shows center string methods in python.

Code:

py1 = "python"
py2 = py1.center(10, "+")
print (py2)

Output:

Python 3 String Methods 3

4. Count

  • Non-overlapping substring occurrences in the range [start, end]. Start and end are optional arguments that are interpreted as slice notation.
  • Python will not count characters twice due to non-overlapping occurrences. The below example shows count string methods in python are as follows.

Code:

py = "Python String Methods"
print(py.count("S"))
print(py.count("o"))
print(py.count("t"))

Output:

Python 3 String Methods 4

5. Encode

  • As a bytes object, returns a string that has been encoded. utf-8 is the standard encoding. To set a custom error handling scheme, errors can be provided.
  • The below example shows that encode string methods in python are as follows.

Code:

from base64 import b64encode
py = "Python"
print(py)
py = b64encode(py.encode())
print(py)

Output:

encode

6. Endswith

  • True is the result. Otherwise, False is returned if the text does not conclude with the required suffix. The test starts at that location if the start parameter is specified. The test ends comparing at that location when we select optional end.
  • The below example shows endswith string methods in python are as follows.

Code:

py = "Python"
print(py.endswith("on"))
print(py.endswith("n"))
print(py.endswith("no", 1, 3))

Output:

Python 3 String Methods 5

7. Expandtabs

  • The specified tab size returns a copy of the string with all tab characters replaced by one or more spaces. Every tabsize character has a position.
  • The below example shows expandtabs string methods in python are as follows.

Code:

py = "py\tth\ton"
print(py)
print(py.expandtabs())
print(py.expandtabs(tabsize=14))
print(py.expandtabs(tabsize=4))

Output:

Python 3 String Methods d

8. Find

  • Returns the string’s lowest index where the substring sub is located within the slice.
  • The below example shows find string methods in python are as follows.

Code:

py = "Python"
print(py.find("P"))
print(py.find("o"))
print(py.find("n"))

Output:

Python 3 String Methods u

9. Format

  • This method applies string formatting. This method can be invoked on a string that contains either literal text or brace-delimited replacement fields.

Code:

print("{} Python {}".format("String", "Methods"))

Output:

h

10. Index

  • If we have not found any substring this string in python will raise an error are as follows.

Code:

py = "Python"
print(py.index("P"))
print(py.index("h"))

Python 3 String Methods

11. Isalnum

  • If the string has all of the characters are alphanumeric, it returns True. Otherwise, returns False.

Code:

py = "Python"
print(py.isalnum())

Output:

true

12. Isalpha

  • If the string has all of the characters are alphabetic, it returns True. Otherwise, returns False.

Code:

py = "Python"
print(py.isalpha())

Output:

true

13. Isdecimal

  • Returns true if the string contains all of the characters are decimal characters. Otherwise, returns False.

Code:

py = "Python"
py1 = "752"
print(py.isdecimal())
print(py1.isdecimal())

Output:

k

14. Isdigit

  • Returns true if the string contains all of the digits. Otherwise, returns False.

Code:

py = "Python"
py1 = "752"
print(py.isdigit())
print(py1.isdigit())

Output:

k

15. Islower

  • Returns true if the string contains all lowercase letters. Otherwise, returns False.

Code:

py = "Python"
py1 = "python"
print(py.islower())
print(py1.islower ())

Output:

k

16. Isupper

  • Returns true if the string contains all uppercase letters. Otherwise, returns False.

Code:

py = "PYTHON"
py1 = "python"
print(py.isupper())
print(py1.isupper())

Output:

false

Examples

Below are the different examples mentioned:

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,064 ratings)

Example #1

The below example shows join python string methods are as follows.

Code:

py = "*"
print(py.join("975"))
py1 = "-"
print(py1.join("python"))

Output:

m

Example #2

The below example shows the isnumeric string method in python which is as follows.

Code:

py = "742"
print(py.isnumeric())
py1 = "Python3"
print(py1.isnumeric())

Output:

false

Example #3

The below example shows isprintable methods in python are as follows.

Code:

py = "\t"
print(py.isprintable())
py1 = "Python3"
print(py1.isprintable())

Output:

k

Example #4

The below example shows isspace string method in python is as follows.

Code:

py = ""
print(py.isspace())
py1 = " "
print(py1.isspace())

Output:

k

Example #5

The below example shows istitle string method in python is as follows.

Code:

py = " Python"
print(py.istitle())
py1 = "python"
print(py1.istitle())

Output:

false

Conclusion

There are multiple string methods are available in python. To concatenate two strings, for instance, use the join method. A string in Python is a collection of Unicode characters surrounded in quotation marks. Python 3 string methods are one of Python’s most common data types.

Recommended Articles

This is a guide to Python 3 string methods. Here we discuss the definition, Python 3 string methods list, examples with code implementation. You may also look at the following articles to learn more-

  1. Python User Defined Exception
  2. Python Z Test
  3. Python Int to String
  4. Python Add List
Popular Course in this category
Python Certifications Training Program (40 Courses, 13+ Projects)
  40 Online Courses |  13 Hands-on Projects |  215+ Hours |  Verifiable Certificate of Completion
4.8
Price

View Course
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

Special Offer - Python Certifications Training Program (40 Courses, 13+ Projects) Learn More