Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Prepending string "@" to usernames

12 views
Skip to first unread message

Thomas Murphy

unread,
May 24, 2013, 6:53:52 PM5/24/13
to Python
Hi beloved list,

I'm having a dumb and SO doesn't seem to have this one answered. I was
sent a long list of instagram usernames to tag for a nightlife
announcement in this format(not real names(i hope))

cookielover93
TheGermanHatesSaurkraut
WhatsThatBoy932834

I'd like to turn this raw text into a list and prepend the @ symbol to
the front of each one, so they're good to go for pasting without me
having to manually add the @ to each one.

Here's where I got to:


raw_address = "cookielover93 TheGermanHatesSaurkraut WhatsThatBoy932834"
address_library = [raw_address.split()]
print address_library

for address in address_library:
final_address = "@" + str(address)
print final_address


However my output is:

[['cookielover93', 'TheGermanHatesSaurkraut', 'WhatsThatBoy932834']]
@['cookielover93', 'TheGermanHatesSaurkraut', 'WhatsThatBoy932834']


I know I'm iterating wrong. May I ask how?

--
Sincerely,
Thomas Murphy
Code Ninja
646.957.6115

Dan Stromberg

unread,
May 24, 2013, 7:00:22 PM5/24/13
to Thomas Murphy, Python
Maybe this is what you're looking for?

raw_address = "cookielover93 TheGermanHatesSaurkraut WhatsThatBoy932834"
address_library = raw_address.split()
print address_library

final_address = []
for address in address_library:
    final_address.append("@" + str(address))
print final_address

Thomas Murphy

unread,
May 24, 2013, 7:04:31 PM5/24/13
to Dan Stromberg, Python
> Maybe this is what you're looking for?
>
> raw_address = "cookielover93 TheGermanHatesSaurkraut WhatsThatBoy932834"
> address_library = raw_address.split()
> print address_library
>
> final_address = []
> for address in address_library:
> final_address.append("@" + str(address))
> print final_address
>
Exactly it. Thank you so much Dan. Perfect!

Andrew Berg

unread,
May 24, 2013, 7:14:27 PM5/24/13
to comp.lang.python
On 2013.05.24 17:53, Thomas Murphy wrote:
> I know I'm iterating wrong. May I ask how?
.split() already returns a list, so instead of iterating over the list and getting a single username, you iterate over the list and get a
single list.
--
CPython 3.3.2 | Windows NT 6.2.9200 / FreeBSD 9.1

Mark Lawrence

unread,
May 24, 2013, 7:16:34 PM5/24/13
to pytho...@python.org
You can safely remove the call to str as address is already a string.

--
If you're using GoogleCrap� please read this
http://wiki.python.org/moin/GoogleGroupsPython.

Mark Lawrence

Tim Chase

unread,
May 24, 2013, 7:19:40 PM5/24/13
to Thomas Murphy, Python
On 2013-05-24 19:04, Thomas Murphy wrote:
>> raw_address = "cookielover93 TheGermanHatesSaurkraut
>> WhatsThatBoy932834" address_library = raw_address.split()
>> print address_library
>>
>> final_address = []
>> for address in address_library:
>> final_address.append("@" + str(address))
>> print final_address
>>
> Exactly it. Thank you so much Dan. Perfect!

Which can be tidily written as a list comprehension:

final_address = ['@' + address for address in raw_address.split()]

-tkc


Thomas Murphy

unread,
May 24, 2013, 8:02:23 PM5/24/13
to Tim Chase, Python
> Which can be tidily written as a list comprehension:
>
> final_address = ['@' + address for address in raw_address.split()]
>
> -tkc
>
>
Ah! Thanks Tim…that tidiness is something I'm trying to head towards
in my own code. I'm trying to transition from the need to visually
write every little step out and write in these more powerful chunks.

Larry Hudson

unread,
May 24, 2013, 11:24:00 PM5/24/13
to
On 05/24/2013 03:53 PM, Thomas Murphy wrote:

<snip>

> Here's where I got to:
>
>
> raw_address = "cookielover93 TheGermanHatesSaurkraut WhatsThatBoy932834"
> address_library = [raw_address.split()]
> print address_library
>
> for address in address_library:
> final_address = "@" + str(address)
> print final_address
>
>
> However my output is:
>
> [['cookielover93', 'TheGermanHatesSaurkraut', 'WhatsThatBoy932834']]
> @['cookielover93', 'TheGermanHatesSaurkraut', 'WhatsThatBoy932834']
>
>
> I know I'm iterating wrong. May I ask how?
>
> --
> Sincerely,
> Thomas Murphy
> Code Ninja
> 646.957.6115
>

No, you're not iterating wrong, but you do have two errors:

1: split() returns a list. You are putting this list (as a single element) inside a list.
Drop the square brackets. Make it: address_library = raw_address.split()

2: In your for loop, you want the print _inside_ the loop not outside. IOW, indent the print
line. The way you have it written it will only print the _last_ string.

-=- Larry -=-

0 new messages