?
Solved

remove 1st few characters in string

Posted on 2014-02-23
5
Medium Priority
?
295 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 29

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 29

Accepted Solution

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

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 29

Expert Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
This article will show, step by step, how to integrate R code into a R Sweave document
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

771 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