Found 27104 Articles for Server Side Programming

How to remove specific characters from a string in Python?

Rajendra Dharmkar
Updated on 11-Aug-2023 10:43:59

1K+ Views

Here, we are considering some code examples to remove specific characters from a string in Python: Using str.replace() In this method, we will use the str.replace() method to replace the specific characters with an empty string. Example We define a string called my_string that contains the original string we want to modify. We use the str.replace() method to replace all occurrences of the character 'o' with an empty string, effectively removing them from the string. We store the resulting string in a new variable called result. We print the resulting string. # define the string my_string = "Lorem Ipsum!" # ... Read More

How to extract numbers from a string in Python?

Rajendra Dharmkar
Updated on 10-Aug-2023 20:54:45

5K+ Views

There are several ways to extract numbers from a string in Python. One way to extract numbers from a string is to use regular expressions. Regular expressions are patterns that you can use to match and manipulate strings. Here's an example code snippet that uses regular expressions to extract all the numbers from a string: Using the re.findall() function Example In this example, we're using the re.findall() function to search for all occurrences of numbers in the text string. The regular expression \d+\.\d+|\d+ matches both floating-point numbers and integers. import re text = "The price of the book is $29.99" ... Read More

Does Python have a string 'contains' substring method?

Rajendra Dharmkar
Updated on 10-Aug-2023 12:02:36

354 Views

In Python, a substring is a sequence of characters that appears within a larger string. For example, in the string "Hello, world!", the substring "world" appears within the larger string. A substring can be a single character, or it can be a longer sequence of characters. Substrings are useful when you need to extract or manipulate specific parts of a string. You can also check if a substring is contained within a larger string using Python's string methods. Python provides a built-in method to check if a string contains a substring. The method is called ‘in’ and it returns a ... Read More

How to convert byte literals to python strings?

Rajendra Dharmkar
Updated on 13-Feb-2020 10:32:41

376 Views

To convert byte literals to Python strings, you need to decode the bytes. It can be done using the decode method on the bytes object.  example>>> b"abcde".decode("utf-8") u'abcde'You can also map bytes to chr if the bytes represent ASCII encoding as follows −bytes = [112, 52, 52] print("".join(map(chr, bytes)))Outputp44

What does the 'b' character do in front of a string literal in Python?

Tarun Chandra
Updated on 25-Oct-2022 14:32:05

10K+ Views

A string is a collection of characters that can represent a single word or a complete phrase. Since you can directly assign strings in Python to a literal (unlike other technologies) it is easy to use them. Python includes a number of built in functions and methods to perform various operations on strings, a string is an object of the String class, which contains these methods. In this article, we are going to find out what is the b character that is present in front of a string literal does in python. The b literal in front of the string ... Read More

How to parse a string to float or int in python?

Rajendra Dharmkar
Updated on 30-Sep-2019 06:50:30

174 Views

To parse a string to int, you can use the following:try:     print int('112') except ValueError:     print 'Cannot parse'This will give you the output:112To parse a string to float, you can use the following:try:     print float('112.15') except ValueError:     print 'Cannot parse'This will give you the output:112.15

How to strip down all the punctuation from a string in Python?

Rajendra Dharmkar
Updated on 11-Aug-2023 11:06:59

867 Views

Stripping down all the punctuation from a string in Python can be achieved using various methods. Here are some examples: Using string.punctuation Example The string.punctuation constant contains all the ASCII punctuation characters. We can use this constant to remove all the punctuation characters from the string. We are using a list comprehension to iterate over each character in the string and check if it's not in the string.punctuation constant. If it's not a punctuation character, we add it to the no_punct variable. import string # sample string with punctuation text = "Lorem, Ipsum!!!" # remove punctuation no_punct = "".join(char for ... Read More

How to check if a string contains only decimal characters?

Tarun Chandra
Updated on 19-Oct-2022 12:25:45

4K+ Views

A string is a group of characters that may be used to represent a single word or an entire sentence. Strings are simple to use in Python since they do not require explicit declaration and may be defined with or without a specifier. In Python strings are represented by the class named string and this class provides several built-in methods to manipulate and access strings. In this article, we are focusing on how to check if a string contains only decimal characters in Python. Using the isdigit() function One way to achieve this is using the inbuilt string function isdigit(). ... Read More

How to get the POST values from serializeArray in PHP?

Ricky Barnes
Updated on 20-Dec-2019 06:20:34

3K+ Views

To get the POST values from serializeArray in PHP, use the serializeArray() method. The serializeArray( ) method serializes all forms and form elements like the .serialize() method but returns a JSON data structure for you to work with.Let’s say we have the PHP content in serialize.php file:ExampleThe following is an example showing the usage of this method:Live Demo           The jQuery Example                              $(document).ready(function() {                         $("#driver").click(function(event){     ... Read More

How to get a string left padded with zeros in Python?

Tarun Chandra
Updated on 19-Oct-2022 12:10:52

16K+ Views

A string is a group of characters that can represent a single word or a whole sentence. In Python there is no need to declare strings explicitly we can directly assign them to a literal. Therefore, easy to use them. A string is an object of the String class, which has multiple methods to manipulate and access strings. In this article, we are going to find out how to get a string left padded with zeros in Python. Using the rjust() function One way to achieve this is using the built-in string function named rjust(). The width argument is the ... Read More

Advertisements