Skip to content
geeksforgeeks
  • 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
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python - Remove String from String List
Next article icon

Python | Removing Initial word from string

Last Updated : 24 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

During programming, sometimes, we can have such a problem in which it is required that the first word from the string has to be removed. These kinds of problems are common and one should be aware about the solution to such problems. Let’s discuss certain ways in which this problem can be solved.

Method #1: Using split() Method

This task can be performed using the split function which performs a split of words and separates the first word of string with the entire words. 

Python3




# Python3 code to demonstrate working of
# Removing Initial word from string
# Using split()
 
# initializing string
test_str = "GeeksforGeeks is best"
 
# printing original string
print("The original string is : " + test_str)
 
# Using split()
# Removing Initial word from string
res = test_str.split(' ', 1)[1]
 
# printing result
print("The string after omitting first word is : " + str(res))
 
 
Output : 
The original string is : GeeksforGeeks is best The string after omitting first word is : is best

Time complexity: O(1)
Auxiliary space: O(1)

Method #2: Using partition() Method 

The partition function is used to perform this particular task in the comparatively lesser internal tasks as compared to the function used in the above method. 

Python3




# Python3 code to demonstrate working of
# Removing Initial word from string
# Using partition()
 
# initializing string
test_str = &quot
GeeksforGeeks is best & quot
 
# printing original string
print(& quot
       The original string is : & quot
       + test_str)
 
# Using partition()
# Removing Initial word from string
res = test_str.partition(' ')[2]
 
# printing result
print(& quot
       The string after omitting first word is : & quot
       + str(res))
 
 
Output : 
The original string is : GeeksforGeeks is best The string after omitting first word is : is best

Method #3: Using join() and split() methods

Python3




# Python3 code to demonstrate working of
# Removing Initial word from string
# Using split() and join()
 
# initializing string
test_str = "GeeksforGeeks is best"
 
# printing original string
print("The original string is : " + test_str)
 
# Using split() and join()
# Removing Initial word from string
x = test_str.split()
res=" ".join(x[1:])
 
# printing result
print("The string after omitting first word is : " + str(res))
 
 
Output
The original string is : GeeksforGeeks is best The string after omitting first word is : is best

Method #4: Using pop() function

Python3




# Python3 code to demonstrate working of
# Removing Initial word from string
# initializing string
test_str = "GeeksforGeeks is best"
 
# printing original string
print("The original string is : " + test_str)
 
x = test_str.split()
x.pop(0)
res = ' '.join(x)
 
# printing result
print("The string after omitting first word is : " + str(res))
 
 
Output
The original string is : GeeksforGeeks is best The string after omitting first word is : is best

Time Complexity: O(N)
Auxiliary Space: O(N)

Method #5: Using index() function

Python3




# Python3 code to demonstrate working of
# Removing Initial word from string
# Using slicing
 
# initializing string
test_str = "GeeksforGeeks is best"
 
# printing original string
print("The original string is : " + test_str)
 
# Removing Initial word from string
# Using slicing
res = test_str[test_str.index(" ")+1:]
 
# printing result
print("The string after omitting first word is : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy
 
 
Output
The original string is : GeeksforGeeks is best The string after omitting first word is : is best

Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(1), as we are just using a few variables for storing intermediate results.

Method 6 : using the find() method and string slicing. 

  1. Initialize the string.
  2. Use the find() method to find the index of the first space character.
  3. Use string slicing to extract the substring after the first space character.
  4. Print the resulting string.

Python3




# Python3 code to demonstrate working of
# Removing Initial word from string
# Using find() method
 
# initializing string
test_str = "GeeksforGeeks is best"
 
# printing original string
print("The original string is : " + test_str)
 
# Removing Initial word from string
# Using find() method
index = test_str.find(' ')
res = test_str[index+1:]
 
# printing result
print("The string after omitting first word is : " + str(res))
 
 
Output
The original string is : GeeksforGeeks is best The string after omitting first word is : is best 

Time complexity: O(n) – where n is the length of the string.
Auxiliary space: O(1) – constant extra space is used.



Next Article
Python - Remove String from String List
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python string-programs
Practice Tags :
  • python

Similar Reads

  • Remove URLs from string in Python
    A regular expression (regex) is a sequence of characters that defines a search pattern in text. To remove URLs from a string in Python, you can either use regular expressions (regex) or some external libraries like urllib.parse. The re-module in Python is used for working with regular expressions. I
    3 min read
  • Python - Remove String from String List
    This particular article is indeed a very useful one for Machine Learning enthusiast as it solves a good problem for them. In Machine Learning we generally encounter this issue of getting a particular string in huge amount of data and handling that sometimes becomes a tedious task. Lets discuss certa
    4 min read
  • Python | Removing strings from tuple
    Sometimes we can come across the issue in which we receive data in form of tuple and we just want the numbers from it and wish to erase all the strings from them. This has a useful utility in Web-Development and Machine Learning as well. Let's discuss certain ways in which this particular task can b
    4 min read
  • Python - Remove substring list from String
    In Python Strings we encounter problems where we need to remove a substring from a string. However, in some cases, we need to handle a list of substrings to be removed, ensuring the string is adjusted accordingly. Using String Replace in a LoopThis method iterates through the list of substrings and
    3 min read
  • Python - Remove suffix from string list
    To remove a suffix from a list of strings, we identify and exclude elements that end with the specified suffix. This involves checking each string in the list and ensuring it doesn't have the unwanted suffix at the end, resulting in a list with only the desired elements. Using list comprehensionUsin
    3 min read
  • Python - Remove Punctuation from String
    In this article, we will explore various methods to Remove Punctuations from a string. Using str.translate() with str.maketrans()str.translate() method combined with is str.maketrans() one of the fastest ways to remove punctuation from a string because it works directly with string translation table
    2 min read
  • Python | Remove prefix strings from list
    Sometimes, while working with data, we can have a problem in which we need to filter the strings list in such a way that strings starting with a specific prefix are removed. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop + remove() + startswith() The combinati
    5 min read
  • Python - Remove leading 0 from Strings List
    Sometimes, while working with Python, we can have a problem in which we have data which we need to perform processing and then pass the data forward. One way to process is to remove a stray 0 that may get attached to a string while data transfer. Let's discuss certain ways in which this task can be
    5 min read
  • Python - Remove after substring in String
    Removing everything after a specific substring in a string involves locating the substring and then extracting only the part of the string that precedes it. For example we are given a string s="Hello, this is a sample string" we need to remove the part of string after a particular substring includin
    3 min read
  • Remove spaces from a string in Python
    Removing spaces from a string is a common task in Python that can be solved in multiple ways. For example, if we have a string like " g f g ", we might want the output to be "gfg" by removing all the spaces. Let's look at different methods to do so: Using replace() methodTo remove all spaces from a
    2 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