remove 1st few characters in string

Posted on 2014-02-23
Last Modified: 2014-03-11
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....
Question by:James Murrell
  • 3
  • 2
LVL 29

Expert Comment

ID: 39881817
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 ( for the details.
LVL 31

Author Comment

by:James Murrell
ID: 39881942
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>
LVL 29

Accepted Solution

pepr earned 500 total points
ID: 39882136
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

LVL 31

Author Closing Comment

by:James Murrell
ID: 39919918
Sorry I have been away. fantastic explanation to help me understand fully... thanks
LVL 29

Expert Comment

ID: 39920161
You are welcome :)

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question