Error encountered while changing case of the letter [duplicate] - python

This question already has answers here:
Why doesn't calling a string method (such as .replace or .strip) modify (mutate) the string?
(3 answers)
Closed last year.
I couldn't lowercase this desired line.I don't know what's the problem with this line?
"name1".lower()
print(name1)

You are not saving the lowercased string to a new variable. Try:
name1 = "name1".lower()
print(name1)

Related

I am using .upper in my defined function , but it do not work to uppercase the string. Can anyone tell me whats wrong in my code? [duplicate]

This question already has answers here:
string.upper(<str>) and <str>.upper() won't execute
(3 answers)
Why doesn't calling a string method (such as .replace or .strip) modify (mutate) the string?
(3 answers)
Closed 8 months ago.
def Upper_case(name):
name.upper()
return name
x=Upper_case('hello')
print(x)
Even though you are calling name.upper() you are not storing it back in name.
Use name = name.upper() instead.

Omitting metacharacters in python [duplicate]

This question already has answers here:
How to write string literals in Python without having to escape them?
(6 answers)
Closed last year.
I want to assing a path to a variable a:
a = "D:\misc\testsets\Real"
How can i omit the \t metacharacter without changing the folder name?
Use raw strings:
a = r"D:\misc\testsets\Real"
Try this:
r denotes raw string.
a = r"D:\misc\testsets\Real"

How to get rid of "+" in a line? [duplicate]

This question already has answers here:
Why doesn't calling a string method (such as .replace or .strip) modify (mutate) the string?
(3 answers)
Closed 3 years ago.
I have the txt file below and I want the remove the "+" if there is a + in front of the number.
+905061459318
+905458507534
+905437335094
I have tried almost all of the solutions that exist in Stackoverflow but still, I cannot remove the + from the line.
The code is here.
with open("numbers.txt") as f:
lines = [line.rstrip('\n') for line in open("numbers.txt")]
for line in lines:
if line.startswith("+"):
line.replace("+","")
else:
pass
You already have line.rstrip('\n'). Make that line.rstrip('\n').lstrip('+').

Why is this split method not working correctly? [duplicate]

This question already has answers here:
why is python string split() not splitting
(3 answers)
Closed 6 years ago.
I'm trying to split
<team>
into just team, here is the code I'm using:
s = "<team>"
s.split(">")[1]
s
'<team>'
s.split(">")[1].split("<")[0]
s
'<team>
As you can see, it's still leaving me with
<team>
anyone know why>
str.split() function returns a list, it does not split the string in place.
You'll need to make a new variable:
s = "<team>"
t = s.split(">")[1]
t

Remove "x" number of characters from a string? [duplicate]

This question already has answers here:
How do I get a substring of a string in Python? [duplicate]
(16 answers)
Understanding slicing
(38 answers)
Closed 8 years ago.
In Python: How do I write a function that would remove "x" number of characters from the beginning of a string?
For instance if my string was "gorilla" and I want to be able remove two letters it would then return "rilla".
OR if my string was "table" and I wanted to remove the first three letters it would return "le".
Please help and thank you everyone!
You can use this syntax called slices
s = 'gorilla'
s[2:]
will return
'rilla'
see also Explain Python's slice notation

Categories

Resources