Python string rearranging [closed] - python

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'

Related

Looking for Words in a List with Similar Letters [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a list with types of cheese and I want to be able to search for gouda by just writing "g" and "o" instead of writing the full sentence.
I've looked for solutions but none are exactly what I am looking for. Maybe this is something common but I just started a week ago with Python I don't know many of the terms.
For some reason I got this cancelled so Im writing this paragraph so the person that answered can answer again
Here is a link to another StackOverflow post I found: Link.
This explains what I think you are looking for in your problem.
This code will print gouda from the wordlist:
wordlist = ['gouda','miss','lake','que','mess']
letters = set('g')
for word in wordlist:
if letters & set(word):
print(word)
All you have to do is set whatever letters you want to search for in the list to the letter variable (in the brackets) and it will return the words that contain the letters you entered.
ex. I added gouda (your example) to this list. If you set the letters variable, to g, it searches the wordlist for any words that contain the letter g, in this case it will return gouda from the wordlist as it is the only word that contains the letter 'g'.
The only downfall of this is if you enter 'ms' to search this wordlist you will get two responses, miss and mess as they both contain letters 'm,s' so in some cases you will have to be more specific if you only want one word to be returned.
Note: this is not my code, I got it from the post linked here, and above.

Looping and Lists - BaSe fOO ThE AttAcK Scramble [closed]

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
In the war against Skynet, humans are trying to pass messages to each other without the computers realising what's happening.
To do this, they are using a simple code:
They read the words in reverse order They only pay attention to the words in the message that start with an uppercase letter So, something like:
BaSe fOO ThE AttAcK contains the message:
attack the base
However, the computers have captured you and forced you to write a program so they can understand all the human messages (we won't go into what terrible tortures you've undergone). Your program must work as follows:
soMe SuPPLies liKE Ice-cREAm aRe iMPORtant oNly tO THeir cReaTORS. tO DestroY thEm iS pOInTLess.
code: soMe SuPPLies liKE Ice-cREAm aRe iMPORtant oNly tO THeir cReaTORS. tO DestroY thEm iS pOInTLess.
says: destroy their ice-cream supplies ​
Notice that, as well as extracting the message, we make every word lowercase so it's easier to read.
Could you please help me with my code? This is my code so far:
output=[]
b=0
d=0
code=input("code: ")
code=code.split()
print(code)
a=len(code)
print(a)
while b<a:
c=code[b]
if c.isupper:
output.append(c)
b=b+1
elif c.islower:
b=b+1
else:
b=b+1
print(output)
I need the last line to say "BaSe ThE AttAck" eliminating "fOO" and I will be reversing the string in the last step to make sense, but it is not differentiating between a lowercase word and an uppercase word.
I have rewritten your code.
#code=input("code: ")
code = "soMe SuPPLies liKE Ice-cREAm aRe iMPORtant oNly tO THeir cReaTORS. tO DestroY thEm iS pOInTLess"
code=code.split()
output = []
for word in reversed(code): #iterate over the list in reverse
if word[0].isupper(): #check if the FIRST letter (word[0]) is uppercase.
output.append(word.lower()) #append word in lowercase to list.
output = " ".join(output) #join the elements of the list together in a string seperated by a space " "
print(output)
output
destroy their ice-cream supplies

Computer lengths of words inside string of a list Python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Im looking for a way to be able to calculate the mean number of letters per each sentence in my list. Im trying to split the strings by white space and then count the length of each word inside but im not able to.
Any guidance would be helpful.
I am not going to write the code for you, but this will probably help. If I am understanding the question correctly you are trying to take a big block of text (paragraph) split it into sentences then get the average len of characters in each sentence.
So:
1) Break text block into sentences. Here is a post that should help you do that.
2) Count the letters in a sentence. Here is another post that will help remove the whitespace from the sentence. If you need to remove all the punctuation (everything except letters) from the string check out this post. Now you have a string that you can simply do a len(sentence_string) to get how many characters are in the sentence.
Next time please post the code you have tried, the errors you have gotten, and the text of the data you are trying to use. DON'T post pictures of words. It makes it a lot harder to help when we can't just copy and paste everything and debug it ourselves.
This code would work:
word = sentence.split(' ')
total = 0
for i in word:
total += len(i)
return float(total)/len(word)
The code splits on whitespace, then adds the length of the words and divides by the number of the words in the sentence. This calculates the average.

Python Username Generator [closed]

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.

cant assign to function call [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
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.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I dont understand why this is happening.. It is not happening with anyone else I know.... Here is my code so far
# creating a string which will be our main sentence
string = input("Please enter a sentence.\n")
str(string)
# creates a list and then splits the string up and puts all the parts into a list
stringlist = []
string.lower()
string = string.split()
stringlist.append(string)
# prints the list to check for any errors during splitting
print(stringlist)
find = input("Which word would you like to find?\n")
while find in stringlist:
index = stringlist.index(find)
stringlist(index) = ""
indexpositions.append(str(index + 1))
What I am trying to do is to find a word in a sentence and find all the indexes of it.
You're using the wrong parentheses to index stringlist. You should do stringlist[index]=""
List of problems I could find
In Python, you need to use [] access the elements of a list. So you need to change your code to
stringlist[index] = ""
indexpositions.append(str[index + 1])
Apart from that,
str(string)
...
string.lower()
are NOOPs. First line simply converts the string to a string object and discards it immediately and the second simply converts the string to lower case string and returns a new string object and that is also ignored. Probably you meant
string = str(string)
...
string = string.lower()
Also, the str(string) part is not necessary, because input function returns a string object only.
Another problem is, string.find, returns -1 if the item is not found in the list. In Python sequences can have negative indexes. So, you may want to be aware of that case as well.
So your code can be written like this
stringlist = input("Please enter a sentence.\n").lower().split()

Categories

Resources