python how to remove http from a string using replace [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 7 years ago.
I would like to remove "http://" in a string that contains a web URL i.e: "http://www.google.com". My code is:
import os
s = 'http://www.google.com'
s.replace("http://","")
print s
I try to replace http:// with a space but somehow it still prints out http://www.google.com
Am i using replace incorrectly here? Thanks for your answer.

Strings are immutable. That means none of their methods change the existing string - rather, they give you back a new one. So, you need to assign the result back to a variable (the same, or a different, one):
s = 'http://www.google.com'
s = s.replace("http://","")
print s

Related

`strip` Python method non-obvious behavior [duplicate]

This question already has answers here:
How do I remove a substring from the end of a string?
(23 answers)
Closed last year.
I was surprised about strip Python method behavior:
>>> 'https://texample.com'.strip('https://')
'example.com'
It was not obvious, because usually I use strip with an one-char argument.
This is because of
The chars argument is a string specifying the set of characters to be removed
(https://docs.python.org/3/library/stdtypes.html#str.strip).
What is the best way to delete a "head" of a string?
you have 3 options:
use string.replace instead of string.strip
startswith method:
if line.startswith("'https://"):
return line[8:]
split:
if "/" in line:
param, value = line.split("/",1)

Is there a way to write single backslash within lists? [duplicate]

This question already has an answer here:
Why does printing a tuple (list, dict, etc.) in Python double the backslashes?
(1 answer)
Closed 1 year ago.
enter image description here
is there a way to print single backslash within list?
Regarding the first version of your question, I wrote this:
First, this expression x='\' isn't right in Python in python. you should rather puth it this way: x='\\', since back slash is a special character in python.
Second, try this:
l=['\\'] print(l)
This will print: ['\\']
But when you execute this: print(l[0]), it renders this '\'. So basically, this ['\\'] is the way to print a backslash within a list.

String format: getting u'' inside the final string [duplicate]

This question already has answers here:
Removing u in list
(8 answers)
Closed 3 years ago.
I have a list of id's and I am trying the following below:
final = "ids: {}".format(tuple(id_list))
For some reason I am getting the following:
"ids: (u'213231231', u'weqewqqwe')
Could anyone help out on why the u is coming inside my final string. When I am trying the same in another environment, I get the output without the u''. Any specific reason for this?
Actually it is unicode strings in python
for literal value of string you can fist map with str
>>> final = "ids: {}".format(tuple(map(str, id_list)))
>>> final
"ids: ('213231231', 'weqewqqwe')

How do I turn a list into a string [duplicate]

This question already has answers here:
Convert a list of characters into a string [duplicate]
(9 answers)
Closed 4 years ago.
I have written a code that ends up outputting what I want but in list format. Just to make it easier to understand, I will make up an input.
If I get
>>>
['H','e','l','l','o',' ','W','o','r','l','d']
as an output, how can I change it to:
>>>
'Hello World'
I have tried using .join() but it tells me that it does not work with lists as an error code.
If you need any more information, or I am being vague, just leave a comment saying so and I will update the question.
And if you leave a downvote, can you at least tell me why so that I can fix it or know what to improve for later posts
You join on the connector like this: ''.join(['H','e','l','l','o',' ','W','o','r','l','d'])
Just use join method by passing a list as parameter.
str = ''.join(['H','e','l','l','o',' ','W','o','r','l','d'])

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!'

Categories

Resources