Combining random strings

This is the place for queries that don't fit in any of the other categories.

Combining random strings

Postby Forlatt » Fri Jan 17, 2014 2:47 pm

Hey guys! I'm new to all this and like many I'm trying to make a Twitterbot in Python.
Now I've found a tutorial which posts line by line from a .txt file; which worked perfectly. But now I'm trying to combine two random strings and I can't find the proper solution.

At the moment I have a code that randomly picks a part and prints it:

Code: Select all
str_a = ['a', 'b', 'c', 'd', 'e']

from random import choice
print choice(str_a)p


But how would I combine str_a with a str_b?

Thanks in advance!
Last edited by metulburr on Fri Jan 17, 2014 3:42 pm, edited 1 time in total.
Reason: initial post lock, and added code tags
Forlatt
 
Posts: 4
Joined: Fri Jan 17, 2014 2:41 pm

Re: Combining random strings

Postby metulburr » Fri Jan 17, 2014 3:46 pm

imports will always go in the beginning of your scripts, unless under special circumstances.

But now I'm trying to combine two random strings and I can't find the proper solution.

assuming that str_b was like such:
Code: Select all
import random
str_a = ['a', 'b', 'c', 'd', 'e']
str_b = ['f', 'g', 'h', 'i', 'j']
print(random.choice(str_a) + random.choice(str_b))
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1407
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Combining random strings

Postby rrashkin » Fri Jan 17, 2014 3:51 pm

In your example, str_a isn't a string per se, it's a list of strings or in this case a list of characters. So, when you ask how to combine str_a with str_b, it appears you're asking about combining 2 lists, and one presumes that would be into a 3rd list comprising the characters of the first two.

Code: Select all
str_a=['a','b','c','d','e']
str_b=['z','y','x','w','v','u',]
str_a+str_b
['a', 'b', 'c', 'd', 'e', 'z', 'y', 'x', 'w', 'v', 'u']


[edit] I just saw metalburr's post. If you meant what he interpreted your question as being, then he's obviously got the answer.
___________________
Bob Rashkin
User avatar
rrashkin
 
Posts: 35
Joined: Tue Feb 12, 2013 4:47 pm

Re: Combining random strings

Postby Yoriz » Fri Jan 17, 2014 3:55 pm

There are many ways to join strings which is best depends on what you are doing
Code: Select all
from random import choice

str_a = ['a', 'b', 'c', 'd', 'e']
str_b = 'was picked'

print choice(str_a), str_b
print ' '.join((choice(str_a), str_b))
print '%s %s' % (choice(str_a), str_b)
print '{} {}'.format(choice(str_a), str_b))

Output
Code: Select all
a was picked
b was picked
c was picked
a was picked
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 782
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Combining random strings

Postby Forlatt » Fri Jan 17, 2014 4:42 pm

Wow, guys! I've never expected to have a helpful reaction this fast, thanks!

Metalburr gave me what I needed, but the other posts are very handy as well. Thanks a lot!
Forlatt
 
Posts: 4
Joined: Fri Jan 17, 2014 2:41 pm

Re: Combining random strings

Postby Forlatt » Fri Jan 17, 2014 5:13 pm

Haha, okay something is wrong. I thought I now knew how to fix this bot, but for some reason it only posts the first letter of str_a

Code: Select all
str_a = ['De stoel', 'De #bijzettafel', 'Het laantje', 'Hij', 'Ons #moeder']
str_b = [' werpt', ' gokt', ' danst', ' kaatst', ' lacht']
str_c = [' voorbij', ' naast', ' op', ' langs', ' onder']
str_d = [' haar', ' hem', ' mij', ' ons', ' het #boek']

f=(random.choice(str_a) + random.choice(str_b) + random.choice(str_c) + random.choice(str_d))

for line in f:
     api.update_status(line)
     print line
     time.sleep(1800)


I hope it's not that much of a problem for a new user to ask these questions.
Last edited by Yoriz on Fri Jan 17, 2014 5:24 pm, edited 1 time in total.
Reason: Added code tags
Forlatt
 
Posts: 4
Joined: Fri Jan 17, 2014 2:41 pm

Re: Combining random strings

Postby Yoriz » Fri Jan 17, 2014 5:24 pm

Its fine to ask these questions but use code tags not quote for posting code.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 782
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Combining random strings

Postby stranac » Fri Jan 17, 2014 5:27 pm

f is a string.
What result did you expect?
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1109
Joined: Thu Feb 07, 2013 3:42 pm

Re: Combining random strings

Postby Yoriz » Fri Jan 17, 2014 5:33 pm

To do that f would have to be an iterable
Code: Select all
import random

str_a = ['De stoel', 'De #bijzettafel', 'Het laantje', 'Hij', 'Ons #moeder']
str_b = [' werpt', ' gokt', ' danst', ' kaatst', ' lacht']
str_c = [' voorbij', ' naast', ' op', ' langs', ' onder']
str_d = [' haar', ' hem', ' mij', ' ons', ' het #boek']

f = (random.choice(str_a), random.choice(str_b), random.choice(str_c),
     random.choice(str_d))

for line in f:
    api.update_status(line)
    print line
    time.sleep(1800)


You should try going through the python tutorial
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 782
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Combining random strings

Postby Forlatt » Fri Jan 17, 2014 6:26 pm

I've got it now! :mrgreen:
Forlatt
 
Posts: 4
Joined: Fri Jan 17, 2014 2:41 pm


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot] and 2 guests