This question already has answers here:
How do I reverse a string in Python?
(19 answers)
Closed 1 year ago.
I need to create a function that reverses a string.
Name your function reverse.
Call your function with the input string 'This is my string'. and assign the result to the variable my_string.
Print out my_string!
You can do like this:
def string_reverse(string_value):
return string_value[::-1]
my_string = "This is my string"
print(string_reverse(my_string))
or you can simply do it this way:
my_string = "This is my string"
print(my_string[::-1])
Related
This question already has answers here:
Split string on whitespace in Python [duplicate]
(4 answers)
Closed 1 year ago.
How do you split a string into a several strings in python. The string I am trying to split is in the format of:
Variable 1
and I would like to split it into:
Variable = Variable
Number = 1
You could split based on the space in the example you gave, unless you need a more generic approach.
# set up your variable
my_var = "variable 1"
# You can split by the space in this instance, if all of your data will look the same
my_split = my_var.split(" ")
# prints variable
print(my_split[0])
# prints 1
print(my_split[1])
This question already has answers here:
How to concatenate (join) items in a list to a single string
(11 answers)
Closed 3 years ago.
How can I make a word from all the letters in a string?
For example:
(this is my list)
["h","e","l","l","o"]
And I want this as output:
hello
Try this :
"".join(["h","e","l","l","o"])
Use the join function which concatenate all the characters/substrings present in the list & return a single string.
name = ["h","e","l","l","o"]
concat_name = "".join(name)
print(concat_name)
Output :
hello
Try this :
''.join(["h","e","l","l","o"])
This question already has answers here:
How do I convert a list into a string with spaces in Python?
(6 answers)
Closed 4 years ago.
say you have a list like so:
lst = ['my', 'name', 'is', 'jack.']
If I convert to a string doing this:
''.join(lst)
output
'mynameisjack.'
How do I make the list print out:
"my name is jack."
instead of all together.
Instead of using ''.join(lst)(an empty string), use ' '.join(lst), with a space (see the documentation of join!).
This question already has answers here:
How do I reverse a string in Python?
(19 answers)
Closed 7 years ago.
i want to reverse the order of characters in a string
so take strings as arguments
and return s new string with letters of the original string
example - "hello" would return "olleh"
All I have gotten to is:
def reverse(" "):
string = input("Give me a word")
x = ord(string)
print(x)
You can do it like this:
"hello"[::-1]
You can read about extended-slices here
This question already has answers here:
How do I escape curly-brace ({}) characters in a string while using .format (or an f-string)?
(23 answers)
Closed 7 years ago.
Ponder that you have a string which looks like the following 'This string is {{}}' and you would like to transform it into the following 'This string is {wonderful}'
if you do 'This string is {{}}'.format('wonderful') it won't work. What's the best way to achieve this?
You just need one more pair of {}
'This string is {{{}}}'.format('wonderful')
you need triple brackets: two for the literal { and }and the pair in the middle for the format function.
print('This string is {{{}}}'.format('wonderful'))
Two brackets to get {} in line (escaping), and third as placeholder:
'This string is {{{}}}'.format('wonderful')
You can do this: print "{{f}}".format(f='wonderful').
You can do this as well: "Hello, {name}!".format(name='John'). This will substitute all {name}s with John.