VOOZH about

URL: https://www.analyticsvidhya.com/blog/2021/07/10-useful-python-string-functions-every-data-scientist-should-know-about/

⇱ 10 Useful Python String Functions for Data Science


India's Most Futuristic AI Conference Is Back – Bigger, Sharper, Bolder

  • d
  • :
  • h
  • :
  • m
  • :
  • s

10 Useful Python String Functions Every Data Scientist Should Know About!

Chirag Goyal Last Updated : 04 Feb, 2025
5 min read

Python string is a built-in type sequence. Strings can be used to handle textual data in Python. Python Strings are immutable sequences of Unicode points. Creating Strings is the simplest and easy to use in Python. To create a string in Python, we simply enclose a text in single as well as double-quotes. Python treats both single and double quotes statements the same. So, In this article, we will be discussing some important and useful Functions of Strings in Python for Data Analysis and Data Manipulation, mainly used in Natural Language Processing(NLP).

To follow this article properly, I assume you are familiar with the basics of Python. If not, I recommend the below popular course given by Analytics Vidhya to get started with the Python Basics:

This article was published as a part of the Data Science Blogathon

Python String Functions

The Python String Functions which we are going to discuss in this article are as follows:

  • capitalize( ) function
  • lower( ) function
  • title( ) function
  • casefold( ) function
  • upper( ) function
  • count( ) function
  • find( ) function
  • replace( ) function
  • swapcase( ) function
  • join( ) function

capitalize( ) function

The capitalize() function returns a string where the first character is the upper case.

Python Code:

#You can see the output of other functions as well

string = "analytics Vidhya is the Largest data science Community"
print(string.capitalize())

Example 2: What happens if the first character is a number instead of a character

string = '10 version of Data Science Blogathon by Analytics Vidhya is very good'
print(string.capitalize())

Output:

10 version of data science blogathon by analytics vidhya is very good

lower( ) function

The lower() function returns a string where all the characters in a given string are lower case. This function doesn’t do anything with Symbols and Numbers i.e, simply ignored these things.

Syntax: string.lower()

Example 1: Lower case the given string

string = "Analytics Vidhya is the Largest Data Science Community"
print(string.lower())

Output:

analytics vidhya is the largest data science community

Example 2: What happens if there is a number instead of a character

string = '10 version of Data Science Blogathon by Analytics Vidhya is very good'
print(string.lower())

Output:

10 version of data science blogathon by analytics vidhya is very good

title( ) function

The title() function returns a string where the first character in every word of the string is an upper case. It is just like a header, or a title.

If in a string any of the words contain either a number or a symbol, then this function converts the first letter after that to upper case.

Syntax: string.title()

Example 1: Make the first letter in each word upper case

string = "analytics vidhya is the Largest data science Community"
print(string.title())

Output:

Analytics Vidhya Is The Largest Data Science Community

Example 2: What happens if there is a number instead of a character

string = '10th version of Data Science Blogathon by Analytics Vidhya is very good'Linkedin
print(string.title())

Output:

10Th Version Of Data Science Blogathon By Analytics Vidhya Is Very Good

casefold( ) function

The casefold() function returns a string where all the characters are lower case.

This function is similar to the lower() function, but the casefold() function is stronger, more aggressive, meaning that it will convert more characters into lower case and will find more matches when comparing two strings and both are converted using the casefold() function.

Syntax: string.casefold()

Example 1: Make the given string to lower case

string = "Analytics Vidhya is the Largest Data Science Community"
print(string.casefold())

Output:

analytics vidhya is the largest data science community

Example 2: What happens if there is a number instead of a character

string = '10th version of Data Science Blogathon by Analytics Vidhya is very good'
print(string.casefold())

Output:

10th version of data science blogathon by analytics vidhya is very good

upper( ) function

The upper() function returns a string where all the characters in a given string are in the upper case.  This function doesn’t do anything with Symbols and Numbers i.e, simply ignored these things.

Syntax: string.upper()

Example 1: Upper case the given string

string = "analytics Vidhya is the Largest Data Science Community"
print(string.upper())

Output:

ANALYTICS VIDHYA IS THE LARGEST DATA SCIENCE COMMUNITY

Example 2: What happens if there is a number instead of a character

string = '10th version of Data Science Blogathon by Analytics Vidhya is very good'
print(string.upper())

Output:

10TH VERSION OF DATA SCIENCE BLOGATHON BY ANALYTICS VIDHYA IS VERY GOOD

count( ) function

The count() function finds the number of times a specified value(given by the user) appears in the given string.

Syntax: string.count(value, start, end)

Example 1: Return the number of times the value “analytics” appears in the string

string = "analytics Vidhya is the Largest Analytics Community"
print(string.count("analytics"))

Output:

1

Example 2: Return the number of times the value “analytics” appears in the string from position 10 to 18

string = "analytics Vidhya is the Largest analytics Community"
print(string.count("analytics", 10, 18))

Output:

0

find( ) function

The find() function finds the first occurrence of the specified value.  It returns -1 if the value is not found in that string.

The find() function is almost the same as the index() function, but the only difference is that the index() function raises an exception if the value is not found.

Syntax: string.find(value, start, end)

Example 1: Where in the text is the first occurrence of the letter “d”?

string = "analytics vidhya is the Largest data science Community"
print(string.find("d"))

Output:

12

Example 2: Where in the text is the first occurrence of the letter “d” when you only search between positions 5 and 16?

string = "analytics vidhya is the Largest data science Community"
print(string.find("d", 5, 16))

Output:

12

Example 3: If the value is not found, the find() function returns -1, but the index() function will raise an exception

string = "analytics vidhya is the Largest data science Community"
print(string.find("d", 5, 10))

Output:

-1

replace( ) function

The replace() function replaces a specified phrase with another specified phrase.

Note: All occurrences of the specified phrase will be replaced if nothing else is specified.

Syntax: string.replace(oldvalue, newvalue, count)

Example 1: Replace all occurrences of the word “science”

string = "analytics vidhya is the Largest data science Community"
print(string.replace("science", "scientists"))

Output:

analytics vidhya is the Largest data scientists Community

Example 2: Replace only the first occurrence of the word “science”

string = "Data science Courses by analytics vidhya are the best courses to learn Data science"
print(string.replace("science", "scientists", 1))

Output:

Data scientists Courses by analytics vidhya are the best courses to learn Data science

swapcase( ) function

The swapcase() function returns a string where all the upper case letters are lower case and vice versa.

Syntax: string.swapcase()

Example 1: Make the lower case letters to upper case and the upper case letters to lower case

string = "analytics vidhya is the Largest data science Community"
print(string.swapcase())

Output:

ANALYTICS VIDHYA IS THE lARGEST DATA SCIENCE cOMMUNITY

Example 2: What happens if there is a number instead of a character

string = '10th version of Data Science Blogathon by Analytics Vidhya is very good'
print(string.swapcase())

Output:

10TH VERSION OF dATA sCIENCE bLOGATHON BY aNALYTICS vIDHYA IS VERY GOOD

join( ) function

The join() function takes all items in an iterable and joins them into one string. We have to specify a  string as the separator.

Syntax: string.join(iterable)

Example 1: Join all the items in a given tuple into a string, using a # (hashtag) character as the separator

myTuple = ("Data Scientists", "Machine Learning", "Data Science")
x = "#".join(myTuple)
print(x)

Output:

Data Scientists#Machine Learning#Data Science

Example 2: Join all the items in a given dictionary into a string, using the word “TEST” as the separator

myDict = {"name": "Analytics Vidhya", "country": "India", "Technology": "Data Science"}
mySeparator = "TEST"
x = mySeparator.join(myDict)
print(x)

Output:

nameTESTcountryTESTTechnology

I hope that you have enjoyed the article. If you like it, share it with your friends also.Something not mentioned or want to share your thoughts? Feel free to comment below And I’ll get back to you. 😉

Thanks for reading!

You can also check my previous blog posts – Previous Data Science Blog posts.

Here is my Linkedin profile in case you want to connect with me. I’ll be happy to be connected with you. For any queries, you can mail me on [email protected]

The media shown in this article are not owned by Analytics Vidhya and are used at the Author’s discretion.

I am a B.Tech. student (Computer Science major) currently in the pre-final year of my undergrad. My interest lies in the field of Data Science and Machine Learning. I have been pursuing this interest and am eager to work more in these directions. I feel proud to share that I am one of the best students in my class who has a desire to learn many new things in my field.

Login to continue reading and enjoy expert-curated content.

Free Courses

Generative AI - A Way of Life

Explore Generative AI for beginners: create text and images, use top AI tools, learn practical skills, and ethics.

Getting Started with Large Language Models

Master Large Language Models (LLMs) with this course, offering clear guidance in NLP and model training made simple.

Building LLM Applications using Prompt Engineering

This free course guides you on building LLM apps, mastering prompt engineering, and developing chatbots with enterprise data.

Improving Real World RAG Systems: Key Challenges & Practical Solutions

Explore practical solutions, advanced retrieval strategies, and agentic RAG systems to improve context, relevance, and accuracy in AI-driven applications.

Microsoft Excel: Formulas & Functions

Master MS Excel for data analysis with key formulas, functions, and LookUp tools in this comprehensive course.

Responses From Readers

Flagship Programs

GenAI Pinnacle Program| GenAI Pinnacle Plus Program| AI/ML BlackBelt Program| Agentic AI Pioneer Program

Free Courses

Generative AI| DeepSeek| OpenAI Agent SDK| LLM Applications using Prompt Engineering| DeepSeek from Scratch| Stability.AI| SSM & MAMBA| RAG Systems using LlamaIndex| Building LLMs for Code| Python| Microsoft Excel| Machine Learning| Deep Learning| Mastering Multimodal RAG| Introduction to Transformer Model| Bagging & Boosting| Loan Prediction| Time Series Forecasting| Tableau| Business Analytics| Vibe Coding in Windsurf| Model Deployment using FastAPI| Building Data Analyst AI Agent| Getting started with OpenAI o3-mini| Introduction to Transformers and Attention Mechanisms

Popular Categories

AI Agents| Generative AI| Prompt Engineering| Generative AI Application| News| Technical Guides| AI Tools| Interview Preparation| Research Papers| Success Stories| Quiz| Use Cases| Listicles

Generative AI Tools and Techniques

GANs| VAEs| Transformers| StyleGAN| Pix2Pix| Autoencoders| GPT| BERT| Word2Vec| LSTM| Attention Mechanisms| Diffusion Models| LLMs| SLMs| Encoder Decoder Models| Prompt Engineering| LangChain| LlamaIndex| RAG| Fine-tuning| LangChain AI Agent| Multimodal Models| RNNs| DCGAN| ProGAN| Text-to-Image Models| DDPM| Document Question Answering| Imagen| T5 (Text-to-Text Transfer Transformer)| Seq2seq Models| WaveNet| Attention Is All You Need (Transformer Architecture) | WindSurf| Cursor

Popular GenAI Models

Llama 4| Llama 3.1| GPT 4.5| GPT 4.1| GPT 4o| o3-mini| Sora| DeepSeek R1| DeepSeek V3| Janus Pro| Veo 2| Gemini 2.5 Pro| Gemini 2.0| Gemma 3| Claude Sonnet 3.7| Claude 3.5 Sonnet| Phi 4| Phi 3.5| Mistral Small 3.1| Mistral NeMo| Mistral-7b| Bedrock| Vertex AI| Qwen QwQ 32B| Qwen 2| Qwen 2.5 VL| Qwen Chat| Grok 3

AI Development Frameworks

n8n| LangChain| Agent SDK| A2A by Google| SmolAgents| LangGraph| CrewAI| Agno| LangFlow| AutoGen| LlamaIndex| Swarm| AutoGPT

Data Science Tools and Techniques

Python| R| SQL| Jupyter Notebooks| TensorFlow| Scikit-learn| PyTorch| Tableau| Apache Spark| Matplotlib| Seaborn| Pandas| Hadoop| Docker| Git| Keras| Apache Kafka| AWS| NLP| Random Forest| Computer Vision| Data Visualization| Data Exploration| Big Data| Common Machine Learning Algorithms| Machine Learning| Google Data Science Agent
👁 Av Logo White

Continue your learning for FREE

Forgot your password?
👁 Av Logo White

Enter OTP sent to

Edit

Wrong OTP.

Enter the OTP

Resend OTP

Resend OTP in 45s

👁 Popup Banner
👁 AI Popup Banner