Solved

remove 1st few characters in string

Posted on 2014-02-23
5
285 Views
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....
0
Comment
Question by:James Murrell
  • 3
  • 2
5 Comments
 
LVL 28

Expert Comment

by:pepr
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.
0
 
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>
0
 
LVL 28

Accepted Solution

by:
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

0
 
LVL 31

Author Closing Comment

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

Expert Comment

by:pepr
ID: 39920161
You are welcome :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now