0

I am pretty new to python and would like to know how to write a program that asks the user to enter a string that contains the letter "a". Then, on the first line, the program should print the part of the string up to and including the certain letter, and on the second line should be the rest of the string. For example...

Enter a word: Buffalo
Buffa 
lo

This is what I have so far :

text = raw_input("Type something: ")
left_text = text.partition("a")[0]
print left_text

So, I have figured out the first part of printing the string all the way up to the certain letter but then don't know how to print the remaining part of the string.

Any help would be appreciated

3
  • 1
    left_text, sep, right_text = text.partition("a") Commented Feb 8, 2019 at 4:15
  • @Hunter text.partition("a")[0] doesn't include the letter 'a' as you requested in your question. Commented Feb 8, 2019 at 4:19
  • 1
    If you are only just learning Python, you should definitely be targeting the currently recommended and supported version of the language, which is Python 3. If you need to maintain legacy Python 2 systems down the line, learning the differences later is completely feasible. By the original timetable, Python 2 was supposed to be end-of-lifed last year, but it got an extension until next year. But it is already clear that experts and library maintainers don't want to support Python 2 any longer. Commented Feb 8, 2019 at 5:18

3 Answers 3

2

If what you want is the first occurrence of a certain character, you can use str.find for that. Then, just cur the string into two pieces based on that index!

In python 3:

split_char = 'a'
text = input()
index = text.find(split_char)
left = text[:-index]
right = text[-index:]
print(left, '\n', right)

I don't have a python2 on hand to make sure, but I assume this should work on python 2:

split_char = 'a'
text = raw_input()
index = text.find(split_char)
left = text[:-index]
right = text[-index:]
print left + '\n' + right)

Another option that is far more concise is to use

left_text, sep, right_text = text.partition("a")
print (left_text + sep, '\n', right_text)

and then as suggested in the comments, thanks @AChampion !

Sign up to request clarification or add additional context in comments.

Comments

0

You should have some knowledge about slicing and concatenating string or list. You can learn them here Slicing and Concatenating

word = raw_input('Enter word:')  # raw_input in python 2.x and input in python 3.x

split_word = raw_input('Split at: ')

splitting = word.partition(split_word)


'''Here lets assume,

   word = 'buffalo'
   split_word = 'a'

   Then, splitting variable returns list, storing three value,
           ['buff', 'a', 'lo']

   To get your desire output you need to do some slicing and concatenate some value .
'''

output = '{}\n{}'.join(splitting[0] + splitting[1], splitting[2])
print(output) 

Comments

0

First find the indices of the character in the given string, then print the string accordingly using the indices.

Python 3

string=input("Enter string")
def find(s, ch):
    return [i for i, ltr in enumerate(s) if ltr == ch]
indices=find(string, "a")

for index in indices[::-1]:
    print(string[:index+1])

print(string[indices[-1]+1:])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.