Skip to content
Advertisement

Tag: string

Find and replace string values in list [duplicate]

This question already has answers here: Apply function to each element of a list (4 answers) Closed 5 months ago. The community reviewed whether to reopen this question 5 months ago and left it closed: Original close reason(s) were not resolved I got this list: What I would like is to replace [br] with some fantastic value similar to <br

Longest common substring from more than two strings

I’m looking for a Python library for finding the longest common sub-string from a set of strings. There are two ways to solve this problem: using suffix trees using dynamic programming. Method implemented is not important. It is important it can be used for a set of strings (not only two strings). Answer These paired functions will find the longest

Split a string at newline characters

I have a string, say How do we split the above with the delimiter n (a newline)? The result should be Answer If you are concerned only with the trailing newline, you can do: See, str.lstrip() and str.strip() for variations. If you are more generally concerned by superfluous newlines producing empty items, you can do:

What is the most efficient string concatenation method in Python?

Is there an efficient mass string concatenation method in Python (like StringBuilder in C# or StringBuffer in Java)? I found following methods here: Simple concatenation using + Using a string list and the join method Using UserString from the MutableString module Using a character array and the array module Using cStringIO from the StringIO module What should be used and

How can I check if a string represents an int, without using try/except?

Is there any way to tell whether a string represents an integer (e.g., ‘3’, ‘-17’ but not ‘3.14’ or ‘asfasfas’) Without using a try/except mechanism? Answer If you’re really just annoyed at using try/excepts all over the place, please just write a helper function: It’s going to be WAY more code to exactly cover all the strings that Python considers

How to replace whitespaces with underscore?

I want to replace whitespace with underscore in a string to create nice URLs. So that for example: Should become I am using Python with Django. Can this be solved using regular expressions? Answer You don’t need regular expressions. Python has a built-in string method that does what you need:

How do I reverse a string in Python?

There is no built in reverse function for Python’s str object. What is the best way of implementing this method? If supplying a very concise answer, please elaborate on its efficiency. For example, whether the str object is converted to a different object, etc. Answer Using slicing: Slice notation takes the form [start:stop:step]. In this case, we omit the start

How to remove the left part of a string?

I have some simple python code that searches files for a string e.g. path=c:path, where the c:path part may vary. The current code is: What is a simple way to get the text after Path=? Answer Starting in Python 3.9, you can use removeprefix:

Why is it string.join(list) instead of list.join(string)?

This has always confused me. It seems like this would be nicer: Than this: Is there a specific reason it is like this? Answer It’s because any iterable can be joined (e.g, list, tuple, dict, set), but its contents and the “joiner” must be strings. For example: Using something other than strings will raise the following error: TypeError: sequence item

Advertisement