Take integer from input with string and integer [duplicate] - python

This question already has answers here:
How to extract numbers from a string in Python?
(19 answers)
Closed 1 year ago.
I want to figure out how to take the integer from a input when there is both a integer and string.
For example, if I input "hello 3", is there a way I can separate the "3" to another variable aside from the "hello"?

Would this work for you:
myInput=input() # Get Input
myString,myIntStr=myInput.split(" ") # Split in to list based on where the spaces are
myInt=int(myIntStr) # Convert to int

Related

How to enter string in python two-dimensional array [duplicate]

This question already has answers here:
Numpy taking only first character of string
(2 answers)
Closed 2 months ago.
Why do I get only the first character in the result?
enter image description here
label = np.empty([17,2],dtype=str)
label[1][1]="asd"
label[2][1]="asd"
print(label)
I don't know if np.empty can input a string
you can use dtype='object' instead of 'str'
test = np.empty((2,2), dtype='object')
test[0,0] = "ashok"
print(test)
Explanation : actually string is object which hold the character array values. so by default indexing in string location the first char not whole string.
example

Adding comma after a character in string [duplicate]

This question already has answers here:
How to add a string in a certain position?
(10 answers)
Closed 7 months ago.
number = input("Enter the number:")
''' Let's suppose the entered number is 0145. Question is below.'''
I want to add a comma after 0.
How can i do this?
Use {:,} to format a number with commas in Python.
Example:
number = 1000
print(f"{number:,}")
Output:
 
1,000
If want a general purpose number formatter for numbers as strings that may include leading 0's then there is a solution using regular expressions here.
If user enters "0145" and want to format that as "0,145" then you'd need to use the string input rather than converting to an integer which would drop any leading 0's.

splitting str into int and then turning into a dictionary [duplicate]

This question already has answers here:
Product code looks like abcd2343, how to split by letters and numbers?
(6 answers)
Split string based on regex
(3 answers)
Closed 2 years ago.
I am trying to concert a list which has words and and points score next to it into a dictionary
the list looks roughly like this ("oliver34", "jack17" , "jane56")
I want to split this into ("oliver", "34" "jack" , "17" , "jane", "56"
so that I can turn them into a dictionary.
if this can be done without importing anything that would be preferable
sorry if this does make sense first time using stack overflow and im not good at coding
If the variables inside the list always have the same format which is a name followed by a number, then you can just do this:
def text_num_split(item):
for index, letter in enumerate(item, 0):
if letter.isdigit():
return [item[:index],item[index:]]

Checking if a number is present in string [duplicate]

This question already has answers here:
Check if a string contains a number
(20 answers)
Closed 2 years ago.
I have a working function that takes an input as string and returns a string. However, I want my function to return an empty string ("") if the input contains any number in whatever position in the string.
For example :
>>> function("hello")
works fine
>>> function("hello1")
should return ""
The main thing you need for that is the method "isdigit()" that returns True if the character is a digit.
For example:
yourstring="hel4lo3"
for char in yourstring:
if char.isdigit():
print(char)
Will output 4 and 3.
I think it is a good exercise for you trying to do your code from that!

How to split a number string using a comma in every 3rd digit from the right [duplicate]

This question already has answers here:
How to print a number using commas as thousands separators
(30 answers)
Add commas into number string [duplicate]
(11 answers)
What's the easiest way to add commas to an integer? [duplicate]
(4 answers)
Adding thousand separator while printing a number [duplicate]
(2 answers)
Closed 2 years ago.
A beginner here! Can I ask how to split a number string in every 3rd digit from the right and put a comma in between and do it again.
>>>num = '12550'
how can I make that into this
>>>12,550
You can use this neat trick:
num = 123456789
print ("{:,.2f}".format(num))
which outputs:
123,456,789.00
If you don't want the decimal places just use:
print ("{:,}".format(num))

Categories