How to make strings upper case in python [duplicate] - python

This question already has answers here:
string.upper(<str>) and <str>.upper() won't execute
(3 answers)
Closed 8 years ago.
I have been looking around for an answer to this and what I could find was you use the .upper() command but this doesn't seem to work,
Example,
>>> x = a
>>>x.upper()
>>>print (x)
But what it displays is just the original.

The correct way to do it is
x = x.upper()
Python strings are immutable, so str.upper() returns the result instead of changing the string in place (as do all other string manipulation functions).

x.upper() does not modify x; it just computes the new string, which you'd see if you printed x.upper(). To retain that value, you could assign it back to x.

Related

Is there a way for python to take in a string with math operators and convert it to an expression? [duplicate]

This question already has answers here:
Evaluating a mathematical expression in a string
(14 answers)
Closed 1 year ago.
I am trying to take a piece of user input: 5+5 or 5*5 but my code receives it as a string. I want to convert it to an expression from which an answer can be deduced. However, I am not sure how to do this.
For that there's a standard function called as eval.
Examples:
>>> eval("5*5")
25
>>> eval("5+5")
10
>>> eval("2+3*4-4")
10
It will take a string and then calculate the output as per the BODMAS Rule.
Simply use "eval", but unsafe.

This python string sorting works but it shouldn't. Should it? [duplicate]

This question already has answers here:
Python Sort() method [duplicate]
(3 answers)
Closed 2 years ago.
Taking an intro to python course, the following code sorts the string words appropriately, but the function does not return sortString, so I'm not understanding why the output is correct. Can you please help me understand?
def sort_words(string):
splitString = string.split()
sortString = splitString.sort()
return splitString
print(sort_words('python is pretty cool'))
Python .sort() returns None, much like print() does. list.sort() works in place - meaning the list is sorted without needing to assign it to another variable name.

From string to fstring [duplicate]

This question already has answers here:
Transform string to f-string
(5 answers)
Closed 2 years ago.
I'm trying to pull a string from JSON, then convert it to an f string to be used dynamically.
Example
Assigned from JSON I get
whose_fault= "{name} started this whole mess"
How to build a lambda to convert it to an f-string and insert the given variable? I just can't quite get my head around it.
I know similar questions have been asked, but no answer seems to quite work for this.
Better question. What's the most pythonic way to insert a variable into a string (which cannot be initially created as an f-string)?
My goal would be a lambda function if possible.
The point being to insert the same variable into whatever string is given where indicated said string.
There is no such thing as f-string type object in python. Its just a feature to allow you execute a code and format a string.
So if you have a variable x= 2020, then you can create another string that contains the variable x in it. Like
y = f"It is now {x+1}". Now y is a string, not a new object type,not a function

How can i combine all the contents of a List to one value Python [duplicate]

This question already has answers here:
Converting a list to a string [duplicate]
(8 answers)
Closed 6 years ago.
If I were to get the input of someone and put it into a list. How would I combine this into one big string.
user_input = input()
listed = list(user_input)
I am having trouble with this since the contents are unknown. Is there anyway to make it one big string again(combining all the contents of the list). Is there anything I can import into my code to do this for me
To join a list together, you can use the join method. Simply use it as a method on whatever string you want to have placed between each entry in the list:
>>> ls = ['Hello,','world!']
>>> ' '.join(ls)
'Hello, world!'

Python - Remove first three chars' of string [duplicate]

This question already has answers here:
Are there limits to using string.lstrip() in python? [duplicate]
(3 answers)
Closed 8 years ago.
So I have a super long string composed of integers and I am trying to extract and remove the first three numbers in the string, and I have been using the lstrip method (the idea is kinda like pop) but sometimes it would remove more than three.
x="49008410..."
x.lstrip(x[0:3])
"8410..."
I was hoping it would just remove 490 and return 08410 but it's being stubborn -_- .
Also I am running Python 2.7 on Windows... And don't ask why the integers are strings. If that bothers you, just replace them with letters. Same thing! LOL
Instead of remove the first 3 numbers, get all numbers behind the third position. You can do it using : operator.
x="49008410..."
x[3:]
>> "8410..."

Categories

Resources