remove 1st few characters in string

Hiya I am new to python and been working on a project for myself... and I am stuck


i need to remove below from the beginning of the string

('The Tweet:', u'RT 

Open in new window


if i do

newstr = result.text.replace("RT", "")

Open in new window


it will remove the RT bit but not the bits with single quotes....

any ideas - please....
LVL 31
James MurrellProduct SpecialistAsked:
Who is Participating?
 
peprConnect With a Mentor Commented:
The result should continue until the paired right parenthesis. Now the explanation: You are using Python 2.x where print is a statement, not a function (the print as the built-in function was introduced in Python 3). This way, the parentheses are not the part of the print call. Instead, the parentheses wrap the elements inside and form a tuple. The first element of the tuple is the string with space, the second element of the tuple is the unicode string with the content of the result.text.

The print statement (in Python 2.x, and the print function in Python 3), call internally a repr() function if the argument is not of a simple type. The built-in repr() function displays the content of an object so that if it was copied into a Python source file, it would produce the instance with the same value. Because of that it prints left parenthesis, then space in single quotes (because Python prefers single quotes for the purpose), then comma and the unicode literal with the RT...

If you used only:
print " ", result.text

Open in new window

you would observed one space for the first argument, then second space as an automatically used separator of arguments, then ' RT <and the rest' without any quotes.

You probably want to get rid of the first four characters: <space>RT<space> and then print the indented tweet text. You can do it like that:
print u'  ' + result.text[4:]

Open in new window

Using the + instead of comma helps to avoid the extra separator space. The u'  ' is used as the + cannot combine non-unicode literal with a unicode value (and the result.text uses unicode). The [4:] returns the tweet text from fourth character on.

You can also check whether you really want to cut the first four characters -- i.e. only when they are <space>RT<space> and only then slice the text:
    text = result.text       # init
    if text.startswith(' RT '):
        text = text[4:]
    print u'  ' + text

Open in new window

0
 
peprCommented:
Your first line displays the beginning of a tuple representation. The single quotes are not part of the values inside the tuple. They are simply delimiters of the string literals. The single quotes and double quotes can be used interchangably in Python. The u in u'RT... means that the string is in Unicode.

Anyway, if 'abc' is a string assigned to the s variable, the s[1:] is another string from s[1] on. It is called slicing. The first number before the colon says the the index of the first element to be copied (here the character as you are working with strings; indexing starts from zero, this way s[1] is the second character), the second number (after the colon) tells the index of the firts elemen that will NOT be the part of the sliced value. If you omit the value, it means or zero (for the first argument), or the length of the sequence (which means slicing all the tail of the sequence).

See Slicing (http://docs.python.org/3/reference/expressions.html?highlight=slicing#slicings) for the details.
0
 
James MurrellProduct SpecialistAuthor Commented:
Wow that went over my head..... if i so line in code could you explain?


here is line

		print (" ",result.text)

Open in new window


result is

(' ', u' RT <then the tweet>
0
 
James MurrellProduct SpecialistAuthor Commented:
Sorry I have been away. fantastic explanation to help me understand fully... thanks
0
 
peprCommented:
You are welcome :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.