string.format() with {} inside string as string [duplicate] - python

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.

Related

How to split my string "a!b!" into a!, b! in python? [duplicate]

This question already has answers here:
Python split() without removing the delimiter [duplicate]
(4 answers)
Closed 11 months ago.
Is it possible to separate the string "a!b!" into two strings "a!" and "b!" and store that in a list? I have tried the split() function (and even with the delimiter "!"), but it doesn't seem to give me the right result that I want. Also, the character "!" could be any character.
How about :
string = 'a!ab!b!'
deliminator = '!'
word_list = [section+deliminator for section in string.split(deliminator) if section]
print(word_list)
Output :
['a!', 'ab!', 'b!']
split() is used when you need to seperate a string with particular character. If you want split a string into half, Try this
s = "a!b!"
l = [s[ : len(s)//2], s[len(s)//2 : ]]
# output : ["a!", "b!"]

How do I reverse this string in python? [duplicate]

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

How to replace the multiple different words with a single character/word in Python? [duplicate]

This question already has answers here:
Better way to remove multiple words from a string?
(5 answers)
Closed 3 years ago.
Note: Without chaining replace method (or) looping the characters in for loop (or) list comprehension
input_string = "the was is characters needs to replaced by empty spaces"
input_string.replace("the","").replace("was","").replace("is","").strip()
output: 'characters needs to replaced by empty spaces'
Is there any direct way to do this?
You can use python regex module(re.sub) to replace multiple characters with a single character:
input_string = "the was is characters needs to replaced by empty spaces"
import re
re.sub("the|was|is","",input_string).strip()
'characters needs to replaced by empty spaces'
This should help..
input_string = "the was is characters needs to replaced by empty spaces"
words_to_replace=['the', 'was','is']
print(input_string)
for words in words_to_replace:
input_string = input_string.replace(words, "")
print(input_string.strip())

converting a string to a readable sentence [duplicate]

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!).

Turning a list into a string or a word [duplicate]

This question already has answers here:
How to concatenate (join) items in a list to a single string
(11 answers)
Closed 7 years ago.
I know in python there is a way to turn a word or string into a list using list(), but is there a way of turning it back, I have:
phrase_list = list(phrase)
I have tried to change it back into a string using repr() but it keeps the syntax and only changes the data type.
I am wondering if there is a way to turn a list, e.g. ['H','e','l','l','o'] into: 'Hello'.
Use the str.join() method; call it on a joining string and pass in your list:
''.join(phrase)
Here I used the empty string to join the elements of phrase, effectively concatenating all the characters back together into one string.
Demo:
>>> phrase = ['H','e','l','l','o']
>>> ''.join(phrase)
'Hello'
Using ''.join() is the best approach but you could also you a for loop. (Martijn beat me to it!)
hello = ['H','e','l','l','o']
hello2 = ''
for character in hello:
hello2 += character

Categories