• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Python / python endswith / 9 Ways to Manipulate Strings in Python

9 Ways to Manipulate Strings in Python

April 1, 2018 by cmdlinetips

Python is great for string manipulation. Python’s string manipulation ability was definitely one of the reasons for its popularity initially. Python’s built-in methods lets you do most common string manipulations. Here are the String object methods in Python and how to use them.

s = 'built-in methods to manipulate strings in Python'

1. How to Check a String Ends With a Suffix?

Python String object’s method endswith is great to see if a string/text ends with a specific suffix. It returns True, when the string ends with the suffix, False otherwise.

s = 'built-in methods to manipulate strings in Python'
>s.endswith('Python')
True
>s.endswith('C++')
False

2. How to Check a String Starts With a Prefix?

Python String object also has a method startswith to see a string/text starts with a specific prefix. It returns True, when the string starts with the prefix, False otherwise.

s = 'built-in methods to manipulate strings in Python'
>s.startswith('built-in')
True

3. How to Find If A Substring Is in a String?

Often you want to check if a string contains a substring. The best way to do that is use ‘in’ statement in Python.

s = 'is this substring in string?'
'substring' in  s
True

4. How To Split a String in Python

Often you want to split a text or string using a delimiter and create a list of words in the string. Python’s split() method lets you split a string. By default, split uses consecutive whitespace in the string as a single separator and gets you a list.

>s = 'How to Split a String in Python'
>s.split()
['How', 'to', 'Split', 'a', 'String', 'in', 'Python']

You can also specify a delimiter to split method. Here, we split the string with the letter ‘a’ as delimiter.

>s = 'How to Split a String in Python'
>s.split('a')
['How to Split ', ' String in Python']

5. How To Join a List of Strings into a String in Python?

Often one wants to join or concatenate list of strings into a single string. Python’s join() method can be used join strings. While joining we can also specify how we want to join with a delimiter.

s = 'How to Join a list of strings?'
s_list = s.split()
" ".join(s_list)

6. How To Remove Whitespaces in a String in Python?

Python has three related methods, strip(), lstrip(),rstrip() to remove whitespaces in strings. Let us see examples of using strip(), lstrip(), rstrip() here.

Let us create a text with white spaces in the beginning and in the end.

s = ' Python strip examples '

strip() method removes both the whitespaces, the ones in the beginning and in the end

>s.strip()
'Python strip examples'

lstrip() method removes only the left whitespaces and thus leaves the one at the end of the string.

>s.lstrip()
'Python strip examples '

rstrip() method removes only the right whitespaces and thus leaves the one at the beginning of the string.

>s.lstrip()
' Python strip examples'

7. How To Convert to Lower Case?

Python String objects’ lower() method is to convert a string into lower case letters.

>s = 'How To Convert a String to Lower Case'
>s.lower()
'how to convert a string to lower case'

8. How To Convert to Upper Case?

Python String objects’ upper() method is for converting a string into upper case. Note all letters in the string will be capitalized.

>s = 'How To Convert a String to Upper Case'
>s.upper()
'HOW TO CONVERT A STRING TO UPPER CASE'

9. How To Convert to Title Case?

Python’s title() method converts a string to a titlecased version, where the first letter of each word is capitalized, while the characters are lowercase.

>s = 'how to convert a string to title case?'
>s.title()
'How To Convert A String To Title Case?'

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default ThumbnailHow to Sort Lists in-place in Python? Default Thumbnail3 Ways to Read a File and Skip Initial Comments in Python Default Thumbnailf-string in Python 3.6 for formatting strings Default Thumbnail3 Ways to Write Text to a File in Python

Filed Under: python endswith, python startswith, python string split, python string strip, Python Tips, String Manipulation in Python Tagged With: python endswith, python startswith, python string split, python string strip, String Manipulation in Python

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version