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 9 years ago.
Improve this question
In Python2.7
I would like to print my phrase like "THIS IS HOW IT SHOULD LOOK LIKE" instead of
"T H I S I S H O W I T S H O U L D L O O K L I K E"
How do I do it?
It sounds like you're doing something like this:
letters = "THIS IS HOW IT SHOULD LOOK LIKE"
for letter in letters:
print letter,
Which will put a space between each letter during printing.
Instead of printing each letter one at a time, print the string itself.
>>>print letters
THIS IS HOW IT SHOULD LOOK LIKE
If letters is a list of characters and not a string, convert it into a string using join.
print "".join(letters)
If you can't get the letters all in one list for whatever reason, you can use sys.stdout.write to print strings without the trailing space or newline.
import sys
letters = "THIS IS HOW IT SHOULD LOOK LIKE"
for letter in letters:
sys.stdout.write(letter)
Result:
THIS IS HOW IT SHOULD LOOK LIKE
Related
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 1 year ago.
Improve this question
currently I faced issue with python code
I have two questions relate with "def" code.
1)
I need to print out 6 alphabet which is "K,E,Y,S,N,O"
slimier like under sample
enter image description here
2)
how can I print out this alphabet if user typing one word like "KEY" then print out *mark with KEY
or If user typing "BED" then print out "E"only because we only have alphabet with K,E,Y,S,N,O
if can anyone help me with this two questions? I appreciate that
Thanks
Question 1 need work with 2-dimensional list (list with lists for rows) and I skip this problem.
As for Question 2 you can filter chars in word.
You can treat string "BED" as list of chars (even without using list("BED")) and you can use it in for-loop to check every char with list ["K","E","Y","S","N","O"] (or "KEYSNO") and skip chars "B" and "D"
#my_chars = "KEYSNO" # also works
my_chars = ["K","E","Y","S","N","O"]
word = "BED"
for char in word:
if char in my_chars:
print(char, "- OK")
else:
print(char, "- unknown")
Result:
B - unknown
E - OK
D - unknown
This way you can create new list to keep only correct chars
my_chars = ["K","E","Y","S","N","O"]
word = "BED"
filtered_chars = []
for char in word:
if char in my_chars:
filtered_chars.append(char)
print(filtered_chars)
Result:
['E']
In Python you can write it even shorter using list comprehension
filtered_chars = [char for char in word if char in my_chars]
Eventually you can write it with function filter() like
filtered_chars = list(filter(lambda char: char in my_chars, word))
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 4 years ago.
Improve this question
For a random string such as:
H!i I am f.rom G3?ermany
how can I move all the special characters to the end of the word, for instance:
Hi! I am from. Germany3?
You can try this one :
s = "H!i I am f.rom G3?ermany"
l = []
for i in s.split():
k = [j for j in i if j.isalpha()]
for m in i:
if not m.isalpha():
k.append(m)
l.append(''.join(k))
print(' '.join(l))
It will o/p like :
"Hi! I am from. Germany3?
In python 2x you can do it in single line like :
k = ' '.join([filter(str.isalpha,i)+''.join([j for j in i if not j.isalpha()]) for i in s.split()])
I'm defining special character as anything thats not a-z, A-Z or spaces:
You can split the string into words, use regex to find the special characters in each word, remove them, add them back to the end of the word, then join the words together to create your new string:
import re
string = "H!i I am f.rom G3?ermany"
words = string.split(' ')
pattern = re.compile('[^a-zA-Z\s]')
new = ' '.join([re.sub(pattern, '', w) + ''.join(pattern.findall(w)) for w in words])
That will turn H!i I am f.rom G3?ermany into Hi! I am from. Germany3?
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
What's the most efficient way to reverse the string and swap the case? For example, if my input string is:
input_string = "Hello Python World"
I want the content of the string to be:
output_string = "DLROw NOHTYp OLLEh"
You may swap the case using str.swapcase() on the reversed string (or vice-versa) as:
input_string = "Hello Python World"
output_string = input_string[::-1].swapcase()
# to reverse the string ^
# OR, input_string.swapcase()[::-1]
where output_string will hold:
>>> output_string
'DLROw NOHTYp OLLEh'
Something like
''.join([c.lower() if c.isupper() else c.upper() for c in my_string][::-1])
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 6 years ago.
Improve this question
I am new to Python. I have the following print statement in python:
print '"' This '"' is xyz'
It prints as follows: " word " is xyz
In other words, it has a white space before first " and a white space each before and after the word 'This'.
My question: how can I change the print statement so that the white space before the first " and white spaces before and after the word 'This' is not printed?
If you meant to print the literal word "This", surrounded by quotes, try this:
print '"This" is xyz'
If you meant to print the variable This, which is bound to the string word, try this:
This = 'word'
print '"{}" is xyz'.format(This)
Is this what you want? You can use \" for a " in a string constant.
>>> print "\"This\" is xyz"
"This" is xyz
>>>
print '"This" is xyz'
print '"This" is xyz'
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
In python, how do you reverse the order of words in a string and also reverse the order of the letters in the word.
For example, if the input is:
Hello world
The output should be:
olleH dlrow
My attempt:
a=input('Line: ')
print(a[::-1])
Your desired output conflicts with the description of your requirements viz "how do you reverse the order of words in a string and also reverse the order of the letters in the word.". That would simply be the same as reversing the string, which you have already provided as your solution. Instead, to reverse the the letters in each word, but retain the order of those words, you can use split() and a reverse slice ([::-1]) on each word.
s = "Hello world"
for word in s.split():
print word[::-1],
Or, like this:
print ' '.join(word[::-1] for word in s.split())
The above assumes that you do not need to retain the exact whitespace between words.
You may try this using the slice option:
def reverseOrder(strs):
return ''.join([strs[i] for i in xrange(len(strs)-1, -1, -1)])
or better try this:
>>> s='Hello World'
>>> ' '.join(w[::-1] for w in s.split())
'olleH dlrow'