How to split a string into a string and an integer? [duplicate] - python

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

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 lowercase a string and also change space into underscore in python? [duplicate]

This question already has answers here:
make upper case and replace space in column dataframe
(3 answers)
How to change upper case letters to lower case letters and spaces to underscores
(2 answers)
Closed 2 years ago.
How can I change a string into lowercase and change space into an underscore in python?
For example, I have a string which Ground Improvement
I want it to be ground_improvement
For now, I know how to that manually use replace.
df['type'] = df['type'].replace('Ground Improvement', 'ground_improvement')
But it's only for ground improvement, I want to make some automation so if there comes any string into the type column, the script will always change into the format I want.
Thankyou.
Use lower and replace on whitespace
df['type'] = df['type'].str.replace(' ','_').str.lower()
input:
df = pd.DataFrame({'type':['Ground Improvement']})
df
looks like this
type
0 Ground Improvement
output
type
0 ground_improvement
Just two lines: -
s = "Ground Improvement"
s = str.lower() # Converts the entire string to lower case
s = str.replace(' ', '_') # Replaces the spaces with _
And There you have ground_improvement stored in s!

Seperate each character in a string and put it in a list [duplicate]

This question already has answers here:
How do I split a string into a list of characters?
(15 answers)
Closed 5 years ago.
How would you go about separating each character in a given input and turn it into a list?
For example I have
import string
print ("Enter string")
x = input("")
Enter string
The quick brown
I want the end result to be
['T','h','e',' ','q','u','i','c','k',' ','b','r','o','w','n']
Y'know, to turn every character as a separate string in a list instead of every word as a separate string.
Thanks!
Simply use list(x) where x is the string.

How to subtract multiple string values from another string [duplicate]

This question already has answers here:
Replacing specific words in a string (Python)
(4 answers)
Closed 6 years ago.
With:
abc = 'abc'
xyz = 'xyz'
word = 'begin abc- middle_xyz_ end'
I need to extract the values of abc and xyz from word.
The result should be
result = 'begin - middle__ end'
How to achieve this with a minimum amount of code?
You use replace() with an empty string as the value to replace with.
result = word.replace('abc','').replace('xyz','')

how do i reverse the letters in a string in python? [duplicate]

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

Categories