Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
The following is my code:
string1 = (input("What is your name?")) #creates a string, stores the name in it
first1 = string1[:1]
string2 = (input("What is your last name?"))
first3 = string2[:3]
from random import randint
sentence = "".join((str(randint(0,9)), first1.lower(), first3.upper()))
print (sentence)
sentence = "".join((str(randint(0,9)), first1.lower(), first3.upper()))
print (sentence)
It works, but I am having some trouble. I need to loop this 5 times - but it doesn't work for some reason!
P.S. Python 3!
You are creating a tuple called sentence, rather than a string
If you change that line to this:
sentence = "".join((str(randint(0,9)), first1.lower(), first3.upper()))
It will create a string that has no gaps, like so when printed:
What is your name?First
What is your last name?Last
5fLAS
You are creating a list, not a string so it seems logical that you get issues when trying to print it...
To append to a string you can do that :
sentence = str(randint(0,9))+(first1.lower())+(first3.upper())
In Python, you don't give a type to your variables, it depends of what you put INTO them.
In Python, elements between parenthesis "()" are considered as TUPLE (a type similar to a list, but that cant be modified), "[]" stands for a list, "{}" stands for a dictionnary. So you must NOT put parts of a string between parenthesis and separated with commas or you will turn them into a list of words. Instead you can use "+" that means to Python that you are making a string concatenation. Also note that i casted your random number into string. If you give 3 strings separated by "+" Python will automatically detect sentence as a String. Wp you've created a string !
If you have a list and want to get rid of the spaces you can also use :
"".join(yourlist)
However i don't recommend it here, you would be creating a list and using a join for nothing since you can create a string from the begining.
Edit: As for the loop, we can't answer if you don't copy paste the code of the loop and the error, it could be a million things.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 13 days ago.
Improve this question
I currently have two lists
songlist = []
artistlist = []
I return
list(zip(songlist, artistlist))
and it gives me what I want(a 3d list of a 2d list containing each song title and respective artist) but before each second item in each list is "\n"
for example a list in the 3d list would look like: ["Amarillo By Morning","\nGeorge Strait"]
I'm gonna be using the artist in a later function so I'd rather there not be the \n there.
instead of my above code i tried returning a variable containing:
[i + j for i, j in zip(songlist, artistlist)]
but that just returned: ["Amarillo By Morning\nGeorge Strait", ... cont.
To avoid making another iterator, you can just modify each artist as they come along, if you can't modify it at the source.
Also, return a list instead of using string concatenation if you want a list.
[[song, artist.strip()] for song, artist in zip(songlist, artistlist)]
Use the strip() method to remove the whitespace characters from the start and the end of the string (including the new line characters) :
zipped_list = list(zip([song.strip() for song in songlist], [artist.strip() for artist in artistlist]))
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I want to implement a function extract_words() that takes in a string of text as its single parameter and returns a list of the words in that string, like this:
input: "There're six words in this string."
output: ["There're", 'six', 'words', 'in', 'this', 'string']
But when I write it like the following and run it in Jupyter Notebook, it shows like this:
I learned how to write this function by the following, so I tried this in Jupyter, and it shows correct:
So may I ask what's wrong with my code, and how to fix it?
The problem is the cast to str inside your function. Just do return inputStr.split() and should works.
def __extract_words__(inputStr):
return inputStr.split()
inputStr = "There're six words in this string."
__extract_words__(inputStr)
There is nothing to replace. Those are escape characters because you returned a string
You want to return the split list as an actual list, not a str() - return inputStr.split()
Look at the docs for the typing module so the type checker would have shown you the issue more clearly. I.e. your function was actually returning a string.
I think what is confusing you is that you needed to use str() when you print the result, but that could be fixed like this
print(f'The list of words is {res}')
That being said, you don't really need your own function if the goal is to only print
test_string = input('text> ')
print(f'The list of words is {test_string.split()}')
Also, the __function_name__ syntax (surrounding underscores) should be reserved for "magic functions" rather than user functions
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
I am wanting to rearrange strings in python, but don understand what functions would be useful in doing so? Can someone help me understand how do this: remove the first letter of a word and place that letter at the end of the word. Then you append "IE" to the end of the word. I would appreciate it, if someone can link to the functions that might be used, so I can learn how they work.
EDIT: I have tried to make this work with a phrase, but I am having issues with getting it to the ie to go at the end of the words. For example HankTIE ouYIE would be the output of the input Thank You.
Here is what I have:
string = input("Please input a word: ")
def silly_encrypter(string):
words = string.split()
for words in string:
first_letter= words[1:] + words[0]
ie_end = first_letter + "IE"
print (ie_end)
silly_encrypter(string)
As other users pointed out, I strongly suggest you to read Python tutorial, which is very friendly and contains a lot of examples that you can try out in your python console.
Having said that, you could take advantage of string indexing plus concatenation to accomplish the things you want (Both things are mentioned in the tutorial):
remove the first letter of a word and place that letter at the end of the word:
s = "myString"
first_letter_at_the_end = s[1:] + s[0]
# If you print `first_at_the_end` you'll get: 'yStringm'
then append "IE" at the end
ie_at_the_end = first_letter_at_the_end + "IE"
# If you print `ie_at_the_end` you'll get: 'yStringmIE'
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I want to split a string without breaking any words in the string. After reading in a text file called "output.txt.", when I use the splitlines() function as below, it breaks the words at the line boundaries (for example, it breaks the word "from" and keeps "fr" on a first line, moving "om" to the next line.
with open('output.txt', 'r') as file:
temp = file.read()
x = ' '.join(temp.splitlines())
I am new to Python; using Python 3.6.8. Is there a way to modify the splitlines() such that it breaks the string at line boundaries and returns a list of splitted strings, but it does not break words in the original string? Thanks.
I don't know if this is the awnser you're looking for, but you can use the .split() function on a string and give a parameter to split on. So if you have:
"Hello this is a string"
and you call .split(" ") on it, it returns:
["Hello", "this", "is", "a", "string"]
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am trying to select a random word that starts with a hashtag, Here is my current code and I have tried random.choice(), but it will only select one letter. I am guessing its because I am storing the results as one big string, maybe it needs a dictionary but I am still a little unsure how to do it.
trends1 = api.trends_place(1) # from the end of your code
# trends1 is a list with only one element in it, which is a
# dict which we'll put in data.
data = trends1[0]
# grab the trends
trends = data['trends']
# grab the name from each trend
names = [trend['name'] for trend in trends]
# put all the names together with a ' ' separating them
trendsName = ' '.join(names)
This returns
#NaFaltaDoQueFazerEu #Aliyaİzzetbegoviç #LawinPH + more
I want to randomly select a hashtag, but at the minute its choosing a random letter from the string, i.e. doing
random.choice(trendsName)
returns a random letter instead of a random hashtag
You would just need to call random.choice() on names instead of trendsName:
import random
random.choice(names)
random.choice() returns an element of a sequence. If your sequence is a string, then that element will be a single character from that string. However, if your sequence is a list, then that element will be a member of the list, which in the case of names, looks like it will be a hashtag.