String without space python

How to split a string without spaces in Python

How to split a string without spaces in Python. If this is the topic you want, this article is for you. I will show you a couple of ways to do that, like using the re.findall() function, the list() function, or the map() function. This article will give you an idea of this topic.

Split a string without spaces in Python

Use the re.findall() function.

  • regex: regular expression to search for digits.
  • string: string you want the regular expression to search for.

The findall() function returns a list containing the pattern matches in the string. If not found, the function returns an empty list.

  • To split a string without spaces into a list of characters, I use the re.findall() function with the RegEx of r’\S’.
import re characterStr = 'learnshareit' # Use the re.findall() function to split a string without spaces into a list of characters. result = re.findall(r'\S', characterStr) print(result)

Also, with the re.findall() function to split the string into a list of integers, I would change RegEx = r’\d’.

import re numberStr = '12345' # Use the re.findall() function to split a string without spaces into a list of integers. result = re.findall(r'\d', numberStr) print(result)

Or, for strings containing uppercase characters without spaces, the re.findall() function also works fine.

import re uppercaseStr = 'VisitLearnShareItWebsite' # Use the re.findall function splits into strings containing uppercase characters. result = re.findall('[A-Z][^A-Z]*', uppercaseStr) print('String containing uppercase characters:', result)
Strings containing uppercase characters: ['Visit', 'Learn', 'Share', 'It', 'Website']

Use the list() function.

Using the list() function to split a string without spaces into lists of characters or lists of integers.

characterStr = 'learnshareit' numberStr = '12345' # Use the list() function to split strings without spaces to list characters and list of integers. characterStrList = list(characterStr) numberList = list(numberStr) print(characterStrList) print(numberList)
['l', 'e', 'a', 'r', 'n', 's', 'h', 'a', 'r', 'e', 'i', 't'] ['1', '2', '3', '4', '5']

Use the map() function.

  • function: The function to execute for each element in the iterable.
  • iterable: the iterable objects you want to traverse.
  • I will split the string without spaces using the map() function.
  • The first parameter, map() is an int() function for the numeric string.
  • The first parameter, map() is the str() function for string literals.
  • Then use the list() function to convert the map object to a list.
characterStr = 'learnshareit' numberStr = '12345' # Split a string of characters without spaces into a list of characters using the map() function. characterList = list(map(str, characterStr)) print(characterList) # Split a string of numbers without spaces into a list of integers using the map() function. characterList = list(map(str, numberStr)) print(characterList)
['l', 'e', 'a', 'r', 'n', 's', 'h', 'a', 'r', 'e', 'i', 't'] ['1', '2', '3', '4', '5']

Summary:

The thread split a string without spaces in Python may end here. You can get the re.findall() function to do this. Please comment below if you have any other questions or ideas for this article.

Читайте также:  Работа с сигналами python

My name is Jason Wilson, you can call me Jason. My major is information technology, and I am proficient in C++, Python, and Java. I hope my writings are useful to you while you study programming languages.

Name of the university: HHAU
Major: IT
Programming Languages: C++, Python, Java

Источник

How To Remove Spaces from a String In Python

How To Remove Spaces from a String In Python

This tutorial provides examples of various methods you can use to remove whitespace from a string in Python.

A Python String is immutable, so you can’t change its value. Any method that manipulates a string value returns a new string.

The examples in this tutorial use the Python interactive console in the command line to demonstrate different methods that remove spaces. The examples use the following string:

s = ' Hello World From DigitalOcean \t\n\r\tHi There ' 
Output
Hello World From DigitalOcean Hi There

This string has different types of whitespace and newline characters, such as space ( ), tab ( \t ), newline ( \n ), and carriage return ( \r ).

Remove Leading and Trailing Spaces Using the strip() Method

The Python String strip() method removes leading and trailing characters from a string. The default character to remove is space.

Declare the string variable:

Use the strip() method to remove the leading and trailing whitespace:

Output
'Hello World From DigitalOcean \t\n\r\tHi There'

If you want to remove only the leading spaces or trailing spaces, then you can use the lstrip() and rstrip() methods.

Remove All Spaces Using the replace() Method

You can use the replace() method to remove all the whitespace characters from the string, including from between words.

Читайте также:  Set java path and java home

Declare the string variable:

Use the replace() method to replace spaces with an empty string:

Output
'HelloWorldFromDigitalOcean\t\n\r\tHiThere'

Remove Duplicate Spaces and Newline Characters Using the join() and split() Methods

You can remove all of the duplicate whitespace and newline characters by using the join() method with the split() method. In this example, the split() method breaks up the string into a list, using the default separator of any whitespace character. Then, the join() method joins the list back into one string with a single space ( » » ) between each word.

Declare the string variable:

Use the join() and split() methods together to remove duplicate spaces and newline characters:

Output
'Hello World From DigitalOcean Hi There'

Remove All Spaces and Newline Characters Using the translate() Method

You can remove all of the whitespace and newline characters using the translate() method. The translate() method replaces specified characters with characters defined in a dictionary or mapping table. The following example uses a custom dictionary with the string.whitespace string constant, which contains all the whitespace characters. The custom dictionary replaces all the characters in string.whitespace with None .

Import the string module so that you can use string.whitespace :

Declare the string variable:

Use the translate() method to remove all whitespace characters:

Output
'HelloWorldFromDigitalOceanHiThere'

Remove Whitespace Characters Using Regex

You can also use a regular expression to match whitespace characters and remove them using the re.sub() function.

This example uses the following file, regexspaces.py , to show some ways you can use regex to remove whitespace characters:

import re s = ' Hello World From DigitalOcean \t\n\r\tHi There ' print('Remove all spaces using regex:\n', re.sub(r"\s+", "", s), sep='') # \s matches all white spaces print('Remove leading spaces using regex:\n', re.sub(r"^\s+", "", s), sep='') # ^ matches start print('Remove trailing spaces using regex:\n', re.sub(r"\s+$", "", s), sep='') # $ matches end print('Remove leading and trailing spaces using regex:\n', re.sub(r"^\s+|\s+$", "", s), sep='') # | for OR condition 

Run the file from the command line:

Читайте также:  Крестики нолики питон pygame

You get the following output:

Remove all spaces using regex: HelloWorldFromDigitalOceanHiThere Remove leading spaces using regex: Hello World From DigitalOcean Hi There Remove trailing spaces using regex: Hello World From DigitalOcean Hi There Remove leading and trailing spaces using regex: Hello World From DigitalOcean Hi There 

Conclusion

In this tutorial, you learned some of the methods you can use to remove whitespace characters from strings in Python. Continue your learning about Python strings.

Want to deploy your application quickly? Try Cloudways, the #1 managed hosting provider for small-to-medium businesses, agencies, and developers — for free. DigitalOcean and Cloudways together will give you a reliable, scalable, and hassle-free managed hosting experience with anytime support that makes all your hosting worries a thing of the past. Start with $100 in free credits!

Источник

Оцените статью