Python Tutorial - Master Python Programming For Beginners from Scratch

  • Home
  • Getting Started
  • Basics
  • OOP
  • Advanced Python
  • Tkinter
Home » Python String Methods

Python String Methods

This section provides you with useful Python string methods that help you manipulate strings more effectively.

Concatenating & Splitting strings

These methods allow you to concatenate strings into one string and split a string into substrings effectively.

MethodDescription
join()Return a string that is the concatenation of strings in an iterable.
concatReturn a string that is the result of a concatenation of two or more strings.
split()Return a list of substring split from the string by a delimiter.

Finding substrings

These methods allow you to locate a substring in a string and return the index of the substring.

MethodDescription
index() Return the index of a substring in a string or raise a ValueError if the substring not found.
find() Return the index of the first occurrence of a substring in a string or -1 if the substring not found.
startswith()Return True if a string starts with another string.
endswith() Return True if a string ends with another string.

Dealing with string cases

These methods help you deal with string cases effectively including lowercase, uppercase, titlecase, and casefold.

MethodDescription
lower()Return a copy of a string in lowercase
upper()Return a copy of a string in uppercase.
title()Return a copy of a string in the titlecase.
capitalize()Return a string with the first letter converted to uppercase and other letters converted to lowercase.
islower()Return True if all cased characters in the string are lowercase and there’s at least one cased character.
istitle()Return True if the string follows the title case rules.
isupper()Return True if all the cased characters in the string are uppercase and there’s at least one cased character.
casefold()Return a casefolded copy of the string.
swapcase()Return a copy of the string with all lowercase characters converted to uppercase and vice versa

Removing unwanted characters

These methods allow you to remove leading and/or trailing unwanted characters from a string and return a new string.

MethodDescription
lstrip()Return a copy of a string with leading characters removed.
rstrip() Return a copy of a string with trailing characters removed.
strip() Return a copy of a string with leading and trailing characters removed.

Replacing substrings

MethodDescription
replace() Return a copy of a string with some or all occurrences of a substring replaced with a new one.

Checking characters in Strings

MethodDescription
isdigit()Return True if all characters in a string are digits.
isdecimal()Return True if all characters in a string are decimal characters.
isnumeric()Return True if all characters in a string are numeric characters.
isalpha()Return True if all characters in a string are alphabetic characters.
isalnum()Return True if all characters in a string are alphanumeric characters.
  • Was this tutorial helpful ?
  • YesNo
Previous Python Basics
Next Python OOP

Getting Started

  • What is Python
  • Install Python
  • Setup VS Code for Python
  • Develop Python Hello World Program

Python Fundamentals

  • Python Syntax
  • Variables
  • Strings
  • Numbers
  • Boolean
  • Constants
  • Comments
  • Type Conversion

Control Flow

  • if…else
  • Ternary Operator
  • for Loop
  • while Loop
  • break
  • continue
  • pass

Functions

  • Python Functions
  • Default Parameters
  • Keyword Arguments
  • Recursive Functions
  • Lambda Expressions
  • Function Docstrings

Python List

  • List
  • Tuple
  • Sorting a List in Place: sort()
  • Sorting a List: sorted()
  • Slicing a List: [::]
  • Unpacking a List
  • Iterating over a List: for loop
  • Finding Index of an Element: index()
  • Iterables
  • Transform List Elements: map()
  • Filtering List Elements: filter()
  • Reducing List Elements: reduce()
  • List Comprehensions

Python Dictionary

  • Dictionary
  • Dictionary Comprehension

Python Set

  • Set
  • Set Comprehension
  • Union of Sets
  • Intersection of Sets
  • Difference between Sets
  • Symmetric Difference of Sets
  • Subset
  • Superset
  • Disjoint Sets

Exception Handling

  • try…except
  • try…except…finally
  • try…except…else

Python Loop with Else Clause

  • for…else
  • while…else
  • do…while Emulation

More on Functions

  • Unpacking Tuples
  • The *args Parameters
  • The **kwargs Parameters
  • Partial Functions

Modules

  • Modules
  • Module Search Path
  • Python __name__
  • Packages

File I/O

  • Reading a Text File
  • Writing to a Text File
  • Creating a Text File
  • Checking If a File Exists
  • Reading from a CSV File
  • Writing to a CSV File

Managing Third-party Packages

  • Python Package Index (PyPI) & PIP
  • Python Virtual Environments
  • Install pipenv on Windows
  • Using pipenv Tool

About pythontutorial.net

Pythontutorial.net helps you master Python programming from scratch fast.

Site Links

  • Home
  • Contact
  • About
  • Privacy Policy

Recent Python Tutorials

  • Tkinter StringVar
  • Tkinter PhotoImage
  • Tkinter OptionMenu
  • Tkinter ScrolledText
  • How to Change the Appearances of Widgets Dynamically Using Ttk Style map() Method

Python References

  • Python String Methods
  • Python MySQL
  • Python PostgreSQL
  • Python Oracle
  • Python SQLite

Copyright © 2021 · by pythontutorial.net. All rights reserved. Log in