1

I have a string like this:

 line = "Student   =  Small   |1-2|   Student"

I want to replace this line to

 line = "StudentShort  =  Small  |1-2|    StudentShort"

The problem is I don't know wheither first and last words are Student or anything else in string. I mean it can be Men, Women, Teacher anything.

I only know that if there is small in string I have to replace first and last word with that name and short

Can some one help?

0

4 Answers 4

2

You want to add "Short" to the first and last word of the string...My advice would be to split and then use indexing and then join!

In [202]: line = "Teacher   =  Small   |1-2|   Student"

In [203]: line = line.split()

In [204]: line[0] += "Short"

In [205]: line[-1] += "Short"

In [206]: line = "  ".join(line)

In [207]: line
Out[207]: 'TeacherShort  =  Small  |1-2|  StudentShort'

I think it would be useful to have this in a function:

def customize_string(string,add_on):
    if "small" in string:
        line = string.split()
        line[0] += add_on
        line[-1] += add_on
        return "  ".join(line)
    else:
        return string

here is using it to show that it works!

In [219]: customize_string(line,"Short")
Out[219]: 'TeacherShort  =  Small  |1-2|  StudentShort'
Sign up to request clarification or add additional context in comments.

6 Comments

The problem is I don't know what are my first and last word in string
Your question is unclear then and this satisfies your example...so what you're saying is that you want to be able to replace an instance without knowing what it is first?
Yes, The problem is I don't know what my first and last word is in the string, it can be teacher, or men or women anything
OH, so your question isn't all cases in student, it is for the first and last word? And is the string formatted where everything is separated by 2 spaces?
str.split won't preserve the irregular spaces in the string.
|
1

You can use the String replace method in python..

http://docs.python.org/2/library/string.html#string.replace

    string.replace(s, old, new[, maxreplace])
    Return a copy of string s with all occurrences of substring
    old replaced by new. If the optional argument maxreplace is 
    given, the first maxreplace occurrences are replaced.

2 Comments

The problem is I don't know what are my first and last word in string
You can find it by splitting the string and getting the first and last word p.split()[0] (give first) splitListCount=len(line.split()); last=line.split()[splitListCount-1];
1

Something like this using regex:

>>> line = "Student   =  Small   |1-2|   Student"
>>> if re.search(r"\bSmall\b",line):
    print re.sub("^(\w+)|(\w+)$",lambda x:x.group()+"Short",line)
'StudentShort   =  Small   |1-2|   StudentShort'

>>> line = "Men   =  Small   |1-2|   Men"
>>> if re.search(r"\bSmall\b",line):
    print re.sub("^(\w+)|(\w+)$",lambda x:x.group()+"Short",line)
'MenShort   =  Small   |1-2|   MenShort'

An improved version of above code(as suggested by @thg435):

def solve(strs, match, word):
    if re.search(r"\b{0}\b".format(match), strs):
         return re.sub(r"(^\w+|\w+$)","\g<0>{0}".format(word), strs)

>>> solve("Men   =  Small   |1-2|   Men", "Small", "Short")
'MenShort   =  Small   |1-2|   MenShort'
>>> solve("Student   =  Small   |1-2|   Student", "Small", "Short")
'StudentShort   =  Small   |1-2|   StudentShort'

6 Comments

The problem is I don't know what are my first and last word in string
Just \1Short instead of lambda.
@thg435 I tried that before using lambda but it returned '\x01Short and with raw string it raised error.
@AshwiniChaudhary: interesting. You might want to change the expr to (^\w+|\w+$) or use \g<0> in replacement to denote the group 0.
you forgot to answer the whole question! He only needs to add "Short" if the string contains "small"
|
0

This selects the "Student" part based on the equals-sign by splitting the string. then it replaces it using line.replace.

line = "Student   =  Small   |1-2|   Student"
name = line.split('=',1)[0].strip()
line = line.replace(name,name+'Short')
print line

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.