Questions tagged [strip]

"strip" refers to the process of removing (stripping) certain characters from a string. A common example is removing trailing whitespace characters.

1330 questions
1206
votes
15 answers

How do I trim whitespace?

Is there a Python function that will trim whitespace (spaces and tabs) from a string? So that given input " \t example string\t " becomes "example string".
Chris
  • 21,549
  • 25
  • 71
  • 99
446
votes
10 answers

How to split by comma and strip white spaces in Python?

I have some python code that splits on comma, but doesn't strip the whitespace: >>> string = "blah, lots , of , spaces, here " >>> mylist = string.split(',') >>> print mylist ['blah', ' lots ', ' of ', ' spaces', ' here '] I would rather end…
Mr_Chimp
  • 6,658
  • 5
  • 37
  • 47
253
votes
8 answers

How can I replace (or strip) an extension from a filename in Python?

Is there a built-in function in Python that would replace (or remove, whatever) the extension of a filename (if it has one)? Example: print replace_extension('/home/user/somefile.txt', '.jpg') In my example: /home/user/somefile.txt would become…
ereOn
  • 53,676
  • 39
  • 161
  • 238
236
votes
14 answers

How to strip all whitespace from string

How do I strip all the spaces in a python string? For example, I want a string like strip my spaces to be turned into stripmyspaces, but I cannot seem to accomplish that with strip(): >>> 'strip my spaces'.strip() 'strip my spaces'
wrongusername
  • 18,564
  • 40
  • 130
  • 214
205
votes
8 answers

String strip() for JavaScript?

How do I strip leading and trailing spaces from a string? For example, " dog " should become "dog".
rawrrrrrrrr
  • 3,647
  • 7
  • 28
  • 33
204
votes
4 answers

Difference between String trim() and strip() methods in Java 11

Among other changes, JDK 11 introduces 6 new methods for java.lang.String class: repeat(int) - Repeats the String as many times as provided by the int parameter lines() - Uses a Spliterator to lazily provide lines from the source string isBlank() -…
Mikhail Kholodkov
  • 23,642
  • 17
  • 61
  • 78
170
votes
8 answers

How to strip leading "./" in unix "find"?

find . -type f -print prints out ./file1 ./file2 ./file3 Any way to make it print file1 file2 file3 ?
breadjesus
  • 1,979
  • 3
  • 13
  • 8
154
votes
8 answers

Remove trailing newline from the elements of a string list

I have to take a large list of words in the form: ['this\n', 'is\n', 'a\n', 'list\n', 'of\n', 'words\n'] and then using the strip function, turn it into: ['this', 'is', 'a', 'list', 'of', 'words'] I thought that what I had written would work, but…
George Burrows
  • 3,391
  • 9
  • 31
  • 31
151
votes
13 answers

How to remove unused C/C++ symbols with GCC and ld?

I need to optimize the size of my executable severely (ARM development) and I noticed that in my current build scheme (gcc + ld) unused symbols are not getting stripped. The usage of the arm-strip --strip-unneeded for the resulting executables /…
Yippie-Ki-Yay
  • 22,026
  • 26
  • 90
  • 148
144
votes
8 answers

Strip spaces/tabs/newlines - python

I am trying to remove all spaces/tabs/newlines in python 2.7 on Linux. I wrote this, that should do the job: myString="I want to Remove all white \t spaces, new lines \n and tabs \t" myString = myString.strip(' \n\t') print myString output: I…
bachurim09
  • 1,681
  • 2
  • 13
  • 14
121
votes
7 answers

Python strip with \n

This is my problem. I'm trying to read a text file and then convert the lines into floats. The text file has \n and \t in it though I don't know how to get rid of it. I tried using line.strip() but it didn't take it off and I got an error when I…
Mallard Duck
  • 1,219
  • 2
  • 8
  • 3
120
votes
2 answers

Change the position of the strip label in ggplot from the top to the bottom?

I know this is not quite a data visualization issue, but the boss asked for it, so I need to figure out if it is possible.
lokheart
  • 23,743
  • 39
  • 98
  • 169
120
votes
13 answers

How can I strip first and last double quotes?

I want to strip double quotes from: string = '"" " " ""\\1" " "" ""' to obtain: string = '" " " ""\\1" " "" "' I tried to use rstrip, lstrip and strip('[^\"]|[\"$]') but it did not work. How can I do this?
Walapa
  • 1,201
  • 2
  • 8
  • 3
109
votes
3 answers

Remove leading and trailing slash / in python

I am using request.path to return the current URL in Django, and it is returning /get/category. I need it as get/category (without leading and trailing slash). How can I do this?
sumit
  • 15,003
  • 12
  • 69
  • 110
107
votes
10 answers

How to remove extra indentation of Python triple quoted multi-line strings?

I have a python editor where the user is entering a script or code, which is then put into a main method behind the scenes, while also having every line indented. The problem is that if a user has a multi line string, the indentation made to the…
Mike
1
2 3
88 89