Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Practice Problems
  • C
  • C++
  • Java
  • Python
  • JavaScript
  • Data Science
  • Machine Learning
  • Courses
  • Linux
  • DevOps
  • SQL
  • Web Development
  • System Design
  • Aptitude
  • GfG Premium
Open In App
Next Article:
7 Useful String Functions in Python
Next article icon

7 Useful String Functions in Python

Last Updated : 11 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

As one of the most popular programming languages, Python enables developers to use string functions that help them to perform various operations. For those who don't know, the string data carries 1 or 1> value (that can be any number, unique character, etc.) and later gets converted into ASCII code i.e. American Standard Code for Information Interchange and Unicode so that the machine can understand in their own language.

Table of Content

  • 7 Useful String Functions in Python 
    • 1.  Capitalize
    • 2. Count
    • 3. Find
    • 4. Lower
    • 5. Upper
    • 6. Replace
    • 7. Join

Python uses the same pattern for its string data which carries out to perform different tasks such as shifting Upper or Lower case, etc. This creates more usefulness among developers to save their time on different tasks without worrying about small typos errors and that's why it is sincerely important for you to have technical knowledge of those string functions that have been narrowed down in this article.

Master Python from beginner to advanced with GeeksforGeeks' comprehensive course! Perfect for aspiring developers, this course covers everything from basic syntax to advanced concepts, helping you become proficient in Python programming.

7 Useful String Functions in Python 

String Functions in Python

1.  Capitalize

The capitalize() is used in Python where the first letter of the string is converted into UPPERCASE and the rest of the characters remain the same. On the other hand, if all the characters are in UPPERCASE then the string will return the same value (except the first character).

Example: mY name is YUVRAJ -> My name is yuvraj

Python
# input from users sentence_1 = "mY name is YUVRAJ" sentence_2 = "MY name is Ansul"  # Convert case using capitalize() capitalized_string = sentence_1.capitalize() print("Sentence 1 output -> ", capitalized_string) capitalized_string = sentence_2.capitalize() print("Sentence 2 output -> ", capitalized_string) 


Output:

Sentence 1 output ->  My name is yuvraj
Sentence 2 output -> My name is ansul

2. Count

The count() is used in Python to count the number of occurrences of an individual element or substring that appears within the string. The count() throws the numeric value that provides the detail of an actual count of a given string.

Example: GFG KARLO HO JAYEGA -> Count of 'G' = 3

Python
message = 'GFG KARLO HO JAYEGA'  # number of occurrence of 'G' print('Number of occurrence of G:', message.count('G')) 


Output:

Number of occurrence of G: 3

3. Find

The find() is used in Python to return the lowest index value from the first occurrence of a string (only in case if its found): else the value would be -1.

Example: Yuvraj is my name -> Position of 'is' = 7

Python
message = 'Yuvraj is my name'  # check the index of 'is' print(message.find('is')) 


Output:

7

Note: If you are confused about how to start learning python, then must refer to the below link:

  • How to Learn Python in 21 Days?

4. Lower

The lower() is used in Python programming to ensure that all the UPPERCASE characters in the string are converted into lowercase and fetched with a new lowercase string and the original copy of the string remains intact.

Example: GEEKSFORGEEKS IS A COMPUTER SCIENCE PORTAL -> 'geeksforgeeks is a computer science portal'

Python
message = 'GEEKSFORGEEKS IS A COMPUTER SCIENCE PORTAL'  # convert message to lowercase print(message.lower()) 


Output:

geeksforgeeks is a computer science portal

5. Upper

The upper() is used in Python programming to ensure that all the lowercase characters in the string are converted into UPPERCASE and fetched with a new string whereas the original copy of the string remains intact.

Example: geeksforgeeks is a computer science portal -> GEEKSFORGEEKS IS A COMPUTER SCIENCE PORTAL

Python
message = 'geeksforgeeks is a computer science portal'  # convert message to uppercase print(message.upper()) 


Output:

GEEKSFORGEEKS IS A COMPUTER SCIENCE PORTAL

6. Replace

The replace() is used in Python to replace any unwanted character or text and replace it with the new desired output within the string. The replace() can be used in Python with the below-mentioned syntax to perform the action:

string.replace(old, new, count)

Example: subway surfer -> Replace 's' with 't' = tubway turfer

Python
text = 'subway surfer'  # replace s with t replaced_text = text.replace('s', 't') print(replaced_text) 


Output:

tubway turfer

7. Join

The join() is used in Python programming to merge each element of an iterable such as a list, set, etc., and later you can use a string separator to separate the values. Thus, join() returns a concatenated string and it will throw a TypeError exception if the iterable contains any non-string element within it.

Python
text = ['Anshul', 'is', 'my', 'only', 'friend']  # join elements of text with space print(' '.join(text)) 


Output:

Anshul is my only friend

Note: If you wish to know more about Python Strings, we recommend you to refer this link: Python String Tutorial.


Next Article
7 Useful String Functions in Python

Y

yuvraj10
Improve
Article Tags :
  • GBlog
  • GBlog 2024

Similar Reads

    Must Know Things to Clear Your Python Coding Interview
    Python is not only one of the most loved languages but rather it is used in various different domains. Due to its high dependency and usage in a lot of fields, there has suddenly been an increase in the job openings in Python programming language and related frameworks. Python developers are in high
    6 min read
    Python String
    A string is a sequence of characters. Python treats anything inside quotes as a string. This includes letters, numbers, and symbols. Python has no character data type so single character is a string of length 1.Pythons = "GfG" print(s[1]) # access 2nd char s1 = s + s[0] # update print(s1) # printOut
    6 min read
    Numpy - String Functions & Operations
    NumPy String functions belong to the numpy.char module and are designed to perform element-wise operations on arrays. These functions can help to handle and manipulate string data efficiently.Table of ContentString OperationsString Information String Comparison In this article, we’ll explore the var
    5 min read
    Python str() function
    The str() function in Python is an in-built function that takes an object as input and returns its string representation. It can be used to convert various data types into strings, which can then be used for printing, concatenation, and formatting. Let’s take a simple example to converting an Intege
    3 min read
    Output of Python programs | Set 7
    Prerequisite - Strings in Python Predict the output of the following Python programs. These question set will make you conversant with String Concepts in Python programming language. Program 1Python var1 = 'Hello Geeks!' var2 = "GeeksforGeeks" print "var1[0]: ", var1[0] # statement 1 print "var2[1:5
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences