Reverse and swap case of the string [closed] - python

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])

Related

how do split a inputted word to letters then pull information from a dictionary from those 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 12 months ago.
Improve this question
How would I return a value from a created dictionary based on inputed text?
Word = input ("input text:")
splitted = split (word)
thisdictionary = {"a": [tuple ([1,0]),tuple ([0,1])}
y = thisdictionary.get (splitted)
print(y)
To change string to a list of letters, just pass the string to list() method.
strSample="se"
strToList= list(strSample)
print(strToList)
I didn't quite get the second part of your question. But if you're asking that each letter is key to the dictionary then you can just loop over list of letters and pass them as dictionary[key].
d = {"s":"First letter", "e":"Second letter"}
for item in strToList:
print(d[item])

How to print the matching characters in the exact same position between two strings? python [closed]

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 2 years ago.
Improve this question
I trying to see how many characters stay in the same exact position from the original string to the string printed backwards.
For example:
string1 = dam
string2 = mad
The program would print "1 matching character". Since "a" stayed in the same position in both strings. How do I do this?
I think this is what you're looking for. It could be made more concise, but here you can clearly see how the count works.
string1 = 'dam'
def count_backwards_matches(string):
stringReverse = string[::-1]
matches = 0
for i in range(len(string)):
if string[i]==stringReverse[i]:
matches += 1
return matches
matches = count_backwards_matches(string1)
print( f"{matches} matching character(s)." )

replacing specified characters with others in of a given string in 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 2 years ago.
Improve this question
I am practicing python so there's a problem:
given strings;
string1 = "abc" ,string2 = "xyz"
I need
result: xyc abc
its with a space.
Try this
string1 = "abc"
string2 = "xyz"
string2 = string2[0:-1]+string1[-1]
res = f'{string2} {string1}'
print(res)
Output
xyc abc
The solution of the specific problem must be :
print(string2 + " " + string1)
String concatenation means add strings together.
Use the + character to add a variable to another variable

Python: Regex and replace random substring [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 need some help with a regular expression in python.
For example:
I have the string:" "substring 1" + ("substring 2" | "substring 3") "... with the number of substrings and any operators.
I want to convert this string to: f (a) + (f (b) | f (c))
That is, replacing the substring with the function containing the parameter is the string that maintains the order of operators.
Is there any way to do this? Thanks
You can try to find and replace any substring between 2 quotes:
import re
s = ' "substring 1" + ("substring 2" | "substring 3") '
print(re.sub(r'("[^"]*")', r'f(\1)', s))

How to reverse the order of letters in a string in python [closed]

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'

Categories

Resources