• We just launched and are currently in beta. Join us as we build and grow the community.

How to to Get a Substring of a String in Python

manya

Token Burn Specialist
M Rep
0
0
0
Rep
0
M Vouches
0
0
0
Vouches
0
Posts
46
Likes
98
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial, we will cover How to Get a Substring of a String in Python. The main goal is to understand the concept of getting a substring of a string. We will provide a sample program to demonstrate the actual coding of this tutorial.

This topic is very easy to understand; just follow the instructions I provide, and you can also do it yourself with ease. The program I will show you is about how to get the substring of a string. I will do my best to provide you with the easiest method of creating this program, called "Get a Substring of a String". So, let's start with the coding.

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Creating Main Function

This is the main function of the application. The following code will display a simple GUI in terminal console of getting a substring from a string. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. while

    True

    :
  2. ret=

    False

  3. print

    (

    "\n

    \n

    ================= Get a Substring of a String =================\n

    \n

    "

    )

  4. my_string =

    "I love Sourcecodester."


  5. print

    (

    my_string[

    2

    :6

    ]

    )


  6. print

    (

    my_string[

    2

    :]

    )


  7. print

    (

    my_string[

    :-1

    ]

    )

  8. user_input=

    input

    (

    "\n

    \n

    Try again? (Yes/No): "

    )
  9. print

    (

    "\n

    \n

    "

    )
  10. if

    user_input.lower

    (

    )

    ==

    'no'

    :
  11. print

    (

    "Exit program."

    )
  12. break

  13. elif

    user_input.lower

    (

    )

    ==

    'yes'

    :
  14. ret=

    True

  15. else

    :
  16. print

    (

    "Wrong input."

    )
  17. ret=

    True


  18. if

    ret:
  19. continued

In the provided code, we first use a while loop to encapsulate all the code, then set a variable for the string we will use. To get the substring of a string, we use square brackets [] and insert a pair of indices to determine the scale you want to substring.

Output:

how-to-to-get-a-substring-of-a-string-in-python-1.png

The How to to Get a Substring of a String in Python source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to to Get a Substring of a String in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,341

323,350

Top