Go Premium for a chance to win a PS4. Enter to Win


remove 1st few characters in string

Posted on 2014-02-23
Medium Priority
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 (http://docs.python.org/3/reference/expressions.html?highlight=slicing#slicings) 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 2000 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

885 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