string maniupulations, oops, how do I replace parts of a string
Posted
by
Joe Gibson
on Stack Overflow
See other posts from Stack Overflow
or by Joe Gibson
Published on 2012-10-30T16:56:37Z
Indexed on
2012/10/30
17:00 UTC
Read the original article
Hit count: 266
I am very new to python. Could someone explain how I can manipulate a string like this?
This function receives three inputs:
complete_fmla: has a string with digits and symbols but has no hyphens ('-') nor spaces.
partial_fmla: has a combination of hyphens and possibly some digits or symbols, where the digits and symbols that are in it (other than hyphens) are in the same position as in the complete_formula.
symbol: one character
The output that should be returned is:
If the symbol is not in the complete formula, or if the symbol is already in the partial formula, the function should return the same formula as the input partial_formula.
If the symbol is in the complete_formula and not in the partial formula, the function should return the partial_formula with the symbol substituting the hyphens in the positions where the symbol is, in all the occurrences of symbol in the complete_formula.
For example:
generate_next_fmla (‘abcdeeaa’, ‘- - - - - - - - ’, ‘d’) should return ‘- - - d - - - -’
generate_next_fmla (‘abcdeeaa’, ‘- - - d - - - - ’, ‘e’) should return ‘- - - d e e - -’
generate_next_fmla (‘abcdeeaa’, ‘- - - d e e - - ’, ‘a’) should return ‘a - - d e e a a’
Basically, I'm working with the definition: def generate_next_fmla (complete_fmla, partial_fmla, symbol):
Do I turn them into lists? and then append? Also, should I find out the index number for the symbol in the complete_fmla so that I know where to append it in the string with hyphens??
© Stack Overflow or respective owner