How to store numbers of a string in a list in python [duplicate] - python-3.x

This question already has answers here:
Convert string (without any separator) to list
(9 answers)
Closed 7 years ago.
I have a string s="12345678"
I need to store these numbers in a list like below format.
s=['1','2','3','4','5','6','7','8']
So can anybody help me how to do this.
Thanks,

Try this:
s = "12345678"
a = [c for c in s]

Related

Groovy : how to fetch last 3 characters of a random length text? [duplicate]

This question already has answers here:
How to get last 2 digit from a String
(7 answers)
Closed 1 year ago.
input :
DEPT MGR_AEG
output :
AEG
above is the input and expected output from piece of Groovy code. I mean I need to fetch the last 3 characters of input string. how to achieve this in Groovy ?
thanks in advance for your help.
reg, Avinash
The simplest way is a substring expression:'
assert 'DEPT MGR_AEG'[-3..-1] == 'AEG'
You can also use take() but it's somewhat unintuative:
assert 'DEPT MGR_AEG'.reverse().take(3).reverse() == 'AEG'

Why does split() return one extra item? [duplicate]

This question already has answers here:
Why are empty strings returned in split() results?
(9 answers)
Closed 2 years ago.
I have this String and I want to separate it using the specified separator. Why is the length 4 instead of 3?
brain = " Know yourself! Understand yourself! Correct yourself! "
brain = brain.strip()
change = brain.split("yourself!")
print(len(change))
Because of your word that you want to split with that (yourself!) is at the end(or at the first) of your string.
you can try this solution :
def is_not_none(element):
if element is not None:
return element
brain = "know yourself! Understand yourself! Correct yourself! "
brain = brain.strip()
brain = list(filter(is_not_none, brain.rsplit('yourself!')))
print(brain)

Apply f-string interpolation to a string [duplicate]

This question already has answers here:
How to postpone/defer the evaluation of f-strings?
(14 answers)
Closed 3 years ago.
Is there a way to achieve applying f-strings interpolation dynamically on a string?
For example given the string a = '{len([1])}'
a.interpolate() to give '1'
Try this:
a = f'{len([1])}'
print(a)
#1

How do I access the first character of a string in Rust? [duplicate]

This question already has answers here:
How do I get the first character out of a string?
(7 answers)
Closed 6 years ago.
In JavaScript, I'd write:
s.charAt(0)
What would it be in Rust? How should I handle the s.length == 0 case?
Use s.chars().next(). This will return None if the string is empty or Some(c) otherwise.

Parse one string to another [duplicate]

This question already has answers here:
How to get the last five characters of a string using Substring() in C#?
(12 answers)
Closed 9 years ago.
string A = "myString";
string B;
Is there a way to initiate B according to the data of A, so that value B changes with A.
B = capture change of A?
Edit: My initial post was not complete and misleading, I found the answer now. Still my question is a duplicate of observer pattern
Try this
B = A.Substring(0,2);
It initates B with substring of A from index 0 and lenght of 2
Sure, using String.Substring:
B = A.Substring(0,2); //"my"
You can try the following example
string A = "myString";
string B;
if (A.StartsWith("my"))
{
B = A.Substring(0, 2);//first two elements of A
}
Check out this Substring method

Resources