Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here
Sign InSign Up

Stackorigin – The Community of Question and Answers

Stackorigin – The Community of Question and Answers Logo Stackorigin – The Community of Question and Answers Logo
Search
Ask A Question

Mobile menu

Close
Ask a Question
Home/ Questions/Q 38736
Next
In Process
sam
  • 0
sam
Asked: April 22, 20222022-04-22T15:12:20+00:00 2022-04-22T15:12:20+00:00In: Programming

How do I get a substring of a string in Python?

  • 0

How do I get a substring of a string in Python?

pythonstring
  • 1 1 Answer
  • 19 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    Related Questions

    • How do you restart Apache?
    • aws s3 sdk returns "You have attempted to create more buckets than allowed"
    • What does ETL mean?
    • Is Apache running on my server?
    • Ways to Check Apache Server Status
    • Alternatives for Apache HTTP Server
    • How to access bitbucket using app password
    • How do I undo the most recent local commits in Git?
    • How do I rename both a Git local and remote branch name?
    • How do I rename a local Git branch?

    1 Answer

    • Voted
    • Oldest
    • Recent
    1. pratap
      2022-04-22T15:15:34+00:00Added an answer on April 22, 2022 at 3:15 pm
      This answer was edited.

      Here is the syntax:

      string[start:end:step]

      Where, start: The starting index of the substring. The character at this index is included in the substring. If start is not included, it is assumed to equal to 0. end: The terminating index of the substring. The character at this index is not included in the substring. If end is not included, or if the specified value exceeds the string length, it is assumed to be equal to the length of the string by default. step: Every “step” character after the current character to be included. The default value is 1. If step is not included, it is assumed to be equal to 1  

      Basic Usage

      string[start:end]: Get all characters from start to end – 1 string[:end]: Get all characters from the beginning of the string to end – 1 string[start:]: Get all characters from start to the end of the string string[start:end:step]: Get all characters from start to end – 1, not including every step character

      Examples

      1. Get the first 5 characters of a string

      string = "freeCodeCamp"
      print(string[0:5])

      Output:

      > freeC

      Note: print(string[:5]) returns the same result as print(string[0:5]) 2. Get a substring 4 characters long, starting from the 3rd character of the string

      string = "freeCodeCamp"
      print(string[2:6])

      Output:

      > eeCo

      3. Get the last character of the string

      string = "freeCodeCamp"
      print(string[-1])

      Output:

      > p

      Notice that the start or end index can be a negative number. A negative index means that you start counting from the end of the string instead of the beginning (from the right to left). Index -1 represents the last character of the string, -2 represents the second to last character and so on. 4. Get the last 5 characters of a string

      string = "freeCodeCamp"
      print(string[-5:])

      Output:

      > eCamp

      5. Get a substring which contains all characters except the last 4 characters and the 1st character

      string = "freeCodeCamp"
      print(string[1:-4])

      Output:

      > reeCode

      6. Get every other character from a string

      string = "freeCodeCamp"
      print(string[::2])

      Output:

      > feCdCm
      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp

    Leave an answer
    Cancel reply

    You must login to add an answer.

    Forgot Password?

    Need An Account, Sign Up Here

    Sidebar

    Ask A Question

    Stats

    • Questions 1k
    • Answers 1k
    • Best Answers 75
    • Users 73

    Adv 250x250

    Adv 234x60

    • Recent
    • Answers
    • zhengwei

      What chemicals are used to clean up chemical spills?

      • 1 Answer
    • zhengwei

      What can you use to absorb motor oil?

      • 1 Answer
    • zhengwei

      How to dispose of used oil absorbents

      • 1 Answer
    • zhengwei

      How to use a spill kit for an oil spill

      • 1 Answer
    • zhengwei

      What is oil spill management?

      • 0 Answers
    • royyu
      royyu added an answer Chemicals are liquid, and when spilled onto the ground they… May 20, 2022 at 6:45 am
    • royyu
      royyu added an answer Engine oil is a mixture of several hydrocarbons, which is… May 19, 2022 at 7:42 am
    • royyu
      royyu added an answer After adsorbing chemical solvents and oil spill pollutants, the adsorbed particles… May 18, 2022 at 7:12 am
    • royyu
      royyu added an answer The best practice for dealing with oil spills is to… May 18, 2022 at 7:09 am
    • hari
      hari added an answer Ubuntu way: To restart: sudo service apache2 restart To stop:… May 13, 2022 at 9:34 am

    New Members

    jessie88

    jessie88

    • 0 Questions
    • 0 Answers
    clarkegable

    clarkegable

    • 0 Questions
    • 0 Answers
    Wellness Resort in Rishikesh

    Wellness Resort in Rishikesh

    • 0 Questions
    • 0 Answers
    Best IVF Hospital in Delhi

    Best IVF Hospital in Delhi

    • 0 Questions
    • 0 Answers
    Mohammedtaufiqumar

    Mohammedtaufiqumar

    • 1 Question
    • 0 Answers

    Adv 234x60

    Trending Categories

    Programming
    802Questions
    , 0Followers
    Technology
    208Questions
    , 3Followers
    General Knowledge
    107Questions
    , 0Followers
    Business & Finance
    81Questions
    , 4Followers
    Health
    72Questions
    , 3Followers

    Trending Tags

    django (94) git (47) google (29) health (21) india (35) mysql (22) oil (38) python (237) usa (25) youtube (23)

    Recent posts

      • On: May 17, 2022

      How To Translate Japanese From Image

      • On: May 17, 2022

      How to Convert Celcius to Fahrenheit 

      • On: May 13, 2022

      How to Start, Stop, or Restart Apache Server on Ubuntu

      • On: May 13, 2022

      How to Install Apache on Ubuntu

      • On: May 5, 2022

      CORS in Django REST Framework

    Explore Our Blog

    Adv 234x60

    Subscribe

    Explore

    • Recent Questions
    • Most Answered
    • Answers
    • No Answers
    • Most Visited
    • Most Voted
    • Random

    Footer

    Stackorigin - The Community of Question and Answers

    Stackorigin

    Stackorigin is the world’s largest Q&A networking site, Stackorigin community brings you the collaboration of all the various Questions and the related Answers given by the community.

    About

    • About Us
    • Contact Us
    • FAQ
    • Guest Post Article

    Info

    • Privacy Policy
    • Terms and Conditions
    • Community Guidelines
    • Tags

    Products

    • Tutorials
    • Advertising
    • Categories
    • Corona
    • StackHow

    Follow

    © 2022 Stackorigin. All Rights Reserved.

    Insert/edit link

    Enter the destination URL

    Or link to existing content

      No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.