Getting certain amount of symbols from string in Python. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Getting certain amount of symbols from string in Python.

Please help with following task. Need to return 2 symbols from middle of the word if it have even symbols and 3 if there is odd number of symbols. Example: #even Input: bottle Output: tt #odd Input: bottles Output: ttl

28th Feb 2020, 10:48 AM
Stanislav Voloshchuk
Stanislav Voloshchuk - avatar
6 Answers
+ 2
You are really supposed to give it a try and post your attempt, before asking others to solve it for you. https://code.sololearn.com/cMudPEWx14Xq/?ref=app
28th Feb 2020, 9:36 PM
Tibor Santa
Tibor Santa - avatar
+ 2
What if the string length was 3 or 1? These are both odd, but for the case of 3, it means the whole string is returned. And if it is only 1 character length, then there's not enough characters to return 3 characters.
28th Feb 2020, 1:31 PM
Ipang
+ 2
Thanks everybody. Especially Jayakrishna, however output of the code is differ from what I trying to solve. What I need is when input abcdef Output should be: cd And when I input: abcdefg Output: cde However thanks for your attention and time.
28th Feb 2020, 7:56 PM
Stanislav Voloshchuk
Stanislav Voloshchuk - avatar
+ 2
Tibor Santa thank you very much for solution from now I post my attempts as well. Thanks for advice.
29th Feb 2020, 7:52 AM
Stanislav Voloshchuk
Stanislav Voloshchuk - avatar
+ 1
In=input() i=len(In) if(i%2==1) : print(In[i//2-1]) print(In[i//2]+In[i//2+1])
28th Feb 2020, 12:39 PM
Jayakrishna 🇮🇳
0
What you tried to get it?
28th Feb 2020, 11:15 AM
Jayakrishna 🇮🇳