This question already has answers here:
Slicing strings in str.format
(6 answers)
Closed 6 years ago.
May be a question that has already been asked:
How can I print an integer in a user definied format.
For example
input = 123456
print(".....".format(input))
where the output should be:
"ab123.45.6"
Unfortunately, slicing in string formating is not possible. So something like:
"ab{0[:2]}.{0[3:4]}.{0[5]}".format(str(input))
would not work.
But is there a better solution than:
"ab{0[0]}{0[1]}{0[2]}.{0[3]}{0[4]}.{0[5]}".format(str(input))
Why not make the slices format() arguments? It looks a bit cleaner.
user_input = str(123456)
print("ab{0}.{1}.{2}".format(user_input[:3], user_input[3:5], user_input[5]))
Related
This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 11 months ago.
Say the string is like a = "['Hello', 'World']". After the conversion, a = ['Hello', 'World'] and the type is a list.
It's called expression evaluation. Read about Python's eval() and literal_eval().
Notice that it may be dangerous, so read the docs carefully.
This question already has answers here:
How do I reverse a string in Python?
(19 answers)
Closed 3 years ago.
I need a simple and effective algorithm to reverse a string (for example: WelCome to emoCleW ).
I tried a loop:
s=input("Enter String To Be Reversed:")
for i in range(len(s)+1,-1,-1):
print(str1[0:i])
but this didn't work for me
Like this:
s=input("Enter String To Be Reversed:")
print(s[::-1])
This question already has answers here:
How to replace multiple substrings of a string?
(28 answers)
Closed 4 years ago.
I currently have the below Python code which works well:-
caseURL = r"\\mydomain\abc\lp_t\GB\123456\Original Format"
caseURL = caseURL.replace("lp_t", "lp_i")
caseURL = caseURL.replace("Original Format", "1")
This works fine as said and carries out the below conversion:-
\\mydomain\abc\lp_t\GB\123456\Original Format
\\mydomain\abc\lp_i\GB\123456\1\
This however just seems a bit clumsy. Is there a more pythonesque way to perform these two segment replacements?
Thanks
A similar post already exists:
How to replace multiple substrings of a string?
You can pick one answer from multiple options in the above post.
This question already has answers here:
Convert a list of characters into a string [duplicate]
(9 answers)
Closed 4 years ago.
I have written a code that ends up outputting what I want but in list format. Just to make it easier to understand, I will make up an input.
If I get
>>>
['H','e','l','l','o',' ','W','o','r','l','d']
as an output, how can I change it to:
>>>
'Hello World'
I have tried using .join() but it tells me that it does not work with lists as an error code.
If you need any more information, or I am being vague, just leave a comment saying so and I will update the question.
And if you leave a downvote, can you at least tell me why so that I can fix it or know what to improve for later posts
You join on the connector like this: ''.join(['H','e','l','l','o',' ','W','o','r','l','d'])
Just use join method by passing a list as parameter.
str = ''.join(['H','e','l','l','o',' ','W','o','r','l','d'])
This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 6 years ago.
It's really odd but I have this string:
"['please', 'help']"
I want something that would get one argument at a time.
I've searched everywhere for this but I didn't find anything.
Thanks in advance
While eval is a correct approach, it can often have negative consequences. I suggest using ast.literal_eval, which is a more safe approach (as mentioned by the linked docs):
import ast
s = "['please', 'help']"
s_list = ast.literal_eval(s)
print s_list
Are you looking for something like this?
string = "['please', 'help']"
string_list = eval(string)
print string_list[0], string_list[1]
Edit: you should ideally use ast.literal_eval as the other answer suggests, if you are unsure of what the string contains.