- How it works
- Homework answers
Answer to Question #282663 in Python for reddy
Distinct Characters
Andy has the word W and he wants to print the unique characters in the word in the order of their occurrence.
write a program to help Andy print the unique characters as described above.
NOTE : consider lower and upper case letters as different
The input is a single line containing the word W
the output should be a single line containing space separated characters
EXPLANATION:
in the example, the word is MATHEMATICS
The unique characters in the given word are M, A, T, H, E, I, C, S, in the order of occurrence
so the output should be M A T H E I C S
sample input1 : MATHEMATICS
sample output1 : M A T H E I C S
sample input2 : banana
sample output 2 : b a n
Need a fast expert's response?
and get a quick answer at the best price
for any assignment or question with DETAILED EXPLANATIONS !
Leave a comment
Ask your question, related questions.
- 1. Given a string write a program to print the mirror of string in Alphabetical order to create a secre
- 2. I have a problem of solving to get the average of 1st quarter, 2nd quarter, 3rd quarter and 4th quar
- 3. I try to fix this code many times but I don't know how to get the average of first quarter, sec
- 4. Number pattern: input: 4Output should :1 2 3 4 17 18 19 20 5 6 7 14 15 16 8 9 12 13
- 5. Modify this function so that it can invert your dictionary. In particular, the function will need to
- 6. Create a Python dictionary that returns a list of values for each key. The key can be whatever type
- 7. Write a loop over the strings in list test_miss and call missing_letters with each string. Print a l
- Programming
- Engineering
Who Can Help Me with My Assignment
There are three certainties in this world: Death, Taxes and Homework Assignments. No matter where you study, and no matter…
How to Finish Assignments When You Can’t
Crunch time is coming, deadlines need to be met, essays need to be submitted, and tests should be studied for.…
How to Effectively Study for a Math Test
Numbers and figures are an essential part of our world, necessary for almost everything we do every day. As important…
Python Substring – How to Slice a String
By Davis David
In Python, a string is a sequence of characters that may contain special characters or alphanumeric characters.
An example of a string is "we meet on Friday at 08:00 am ". And you can access specific sub-parts of the string commonly known as substrings.
We can define a substring as a sequence of characters within a string. From the previous example, Python substrings can be "Friday", "at", and "meet", for example.
How to Generate a Substring in Python
Python provides different ways and methods to generate a substring, to check if a substring is present, to get the index of a substring, and more.
You can extract a substring from a string by slicing with indices that get your substring as follows:
string[start:stop:step]
- start - The starting index of the substring.
- stop - The final index of a substring.
- step - A number specifying the step of the slicing. The default value is 1.
Indices can be positive or negative numbers. The positive indices start from the beginning and go to the end of the string, and the negative indices start from the end and go to the beginning of a string.
In this article, you will learn how to perform various operations related to substrings in Python.
How to Get the First n Characters of a String in Python
This example will show you how to slice the first 5 characters from the string.
Here you define the stop index which is 5. The start index is by default 0.
The output is ‘hello’ .
How to Get the Middle Characters of a String via Python Substrings
This example will show you how to slice characters from index 3 to index 5 from the string.
The output is 'lo’ .
How to Get the Last Character of a String in Python
To get the last character use the -1 index (negative index). Check out the following example:
The output will be ‘p’ .
How to Get the Last n Characters of a String in Python
In this example, you will slice the last 4 characters from the string. Here you use the negative index to start slicing from the end of the string.
The output will be ‘camp’ .
How to Slice the String with Steps via Python Substrings
You can slice the string with steps after indicating a start-index and stop-index. By default, the step is 1 but in the following example, the step size is 2.
The output will be ‘wloet fecdcm’ .
How to Check if a Substring is Present Within a String in Python
Sometimes you want to check if a substring is present in a string. The following example will validate if the substring ‘code’ is in the string:
If present, it will return True, otherwise False.
Here, the output will be True .
Another Way to Check if the Python Substring is Present in the String
You can use the find() method to check if a substring is present in the string.
Let’s check the following example:
If it's available it returns the left-most index of the substring, otherwise it returns -1 (which means it's not available).
Here the output is -1 , which means “zz” is not present in “hello world”.
How to Get the Character of a Given Index in a String in Python
You can choose to slice a specific character according to its index number.
The output will be ‘O’ .
How to Create a List of Substrings from a String in Python
You can use the split() method to create a list of substrings. Let’s check out the following example:
The output will be ['welcome', 'to', 'freecodecamp', 'platform']
How to Reverse a String in Python with Negative Steps
To reverse the string, the step must be a negative value, for example -1.
The output is ‘pmacedoceerf ot emoclew’ .
How to Count How Many Times a Substring is Present in a String in Python
You can use the count() method to know how many times a particular substring is in a string.
The output is 1.
Final Thoughts on Python Substrings
Congratulations 👏👏, you have made it to the end of this article! I hope you have learned something new about Python substrings.
If you learned something new or enjoyed reading this article, please share it so that others can see it. Until then, see you in the next post!
You can also find me on Twitter @Davis_McDavid .
And you can read more articles like this here
If you read this far, thank the author to show them you care. Say Thanks
Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started
IMAGES
VIDEO