Found 27104 Articles for Server Side Programming

How to change any data type into a string in Python?

Pythonic
Updated on 30-Jul-2019 22:30:20

176 Views

Any built-in data type converted into its string representation by str() function >>> str(10) '10' >>> str(11.11) '11.11' >>> str(3+4j) '(3+4j)' >>> str([1,2,3]) '[1, 2, 3]' >>> str((1,2,3)) '(1, 2, 3)' >>> str({1:11, 2:22, 3:33}) '{1: 11, 2: 22, 3: 33}' For a user defined class to be converted to string representation, __str__() function needs to be defined in it. >>> class rectangle: def __init__(self): self.l=10 self.b=10 def __str__(self): return 'length={} breadth={}'.format(self.l, self.b) >>> r1=rect() >>> str(r1) 'length = 10 breadth = 10'

What are different data conversion methods in Python?

Sarika Singh
Updated on 14-Nov-2022 08:22:29

610 Views

Type conversion is the transformation of a Python data type into another data type. Implicit type conversion and explicit type conversion are the two basic categories of type conversion procedures in Python. We will cover the following topics in this article − Implicit Type Conversion in Python is carried out by the Python interpreter automatically. In Python, explicit type conversion must be performed directly by the programmer. Let's study more about these two approaches in depth and with some illustrations. Implicit Type conversions Implicit type conversion occurs when the Python interpreter automatically changes an object's data type without ... Read More

How to print concatenated string in Python?

Pythonic
Updated on 30-Jul-2019 22:30:20

340 Views

When used with strings, plus (+) is defined as concatenation operator. It appends second string to the first string. >>> s1 = 'TutorialsPoint ' >>> s2 = 'Hyderabad' >>> print (s1+s2) TutorialsPoint Hyderabad

How to print a string two times with single statement in Python?

Pythonic
Updated on 30-Jul-2019 22:30:20

172 Views

When used with strings, asterisk (*) is defined as repetition operator. It concatenates given string as many times as number followed by asterisk. >>> string = 'abcdefghij' >>> print (string*2) abcdefghijabcdefghij

How to divide a string by line break or period with Python regular expressions?

Rajendra Dharmkar
Updated on 16-Dec-2019 08:57:39

295 Views

The following code splits given string by a period and a line break as followsExampleimport re s = """Hi. It's nice meeting you. My name is Jason.""" result = re.findall(r'[^\s\.][^\.]+', s) print resultOutputThis gives the following output['Hi', "It's nice meeting you", 'My name is Jason']

How can I match the start and end in Python's regex?

Rajendra Dharmkar
Updated on 08-Sep-2023 13:51:19

593 Views

Have you ever come across situations where you had to determine if a string starts or ends with a particular pattern in Python? If so, you don’t need to worry, for Python's regular expressions provide a solution in such cases. With the power of regex, you can easily check whether a string begins or concludes with a specific sequence of characters. In this comprehensive article, we will explore various code examples that demonstrate how to use Python's regex to accomplish this task effortlessly. Before taking a plunge into the examples, let's take a break and understand what regular expressions are ... Read More

How do I do a case-insensitive string comparison in Python?

Rajendra Dharmkar
Updated on 08-Sep-2023 13:58:17

1K+ Views

Have you ever encountered a situation where you needed to compare strings in Python but wanted to ignore the differences in letter casing? Worry not, for Python provides straightforward solutions to achieve case−insensitive string comparisons. In this comprehensive guide, we will explore five different code examples, each showcasing a unique method for conducting case−insensitive string comparisons. So, let's dive in and discover the wonders of Python's string comparison versatility! Before we delve into the examples, let's briefly understand what a case−insensitive string comparison entails. When comparing strings, a case−insensitive approach treats uppercase and lowercase letters as equal, thereby disregarding the ... Read More

How to search and replace text in a file using Python?

Rajendra Dharmkar
Updated on 22-Aug-2023 10:15:24

8K+ Views

The manipulation of files in the domain of programming plays a decisive role in data processing and management. Developers find themselves equipped with robust tools to handle files and text effectively in Python which is a multipurpose and powerful language. Among the everyday tasks involving files, one crucial operation is the search and replacement of specific text patterns with desired content. There are multiple approaches to accomplish this task, ranging from simple string manipulation to the use of powerful regular expressions in Python. In this exhaustive article, we will explore a few practical code examples that demonstrate various techniques to ... Read More

What is the difference between ++i and i++ in c?

Jayashree
Updated on 12-Sep-2023 02:57:07

32K+ Views

In C, ++ and -- operators are called increment and decrement operators. They are unary operators needing only one operand. Hence ++ as well as -- operator can appear before or after the operand with same effect. That means both i++ and ++i will be equivalent.i=5; i++; printf("%d", i);and i=5 ++i; printf("%d", i);both will make i=6.However, when increment expression is used along with assignment operator, then operator precedence will come into picture. i=5; j=i++;In this case, precedence of = is higher than postfix ++. So, value of i is assigned to i before incrementing i. Here j becomes 5 and i becomes 6.i=5; ... Read More

What is the difference between Python's re.search and re.match?

Rajendra Dharmkar
Updated on 22-Aug-2023 10:25:01

712 Views

In the vast realm of Python programming, regular expressions stand as essential tools for text processing and pattern matching, unlocking a plethora of possibilities for developers. Within Python's re module, two prominent functions, re.search() and re.match(), emerge as the veritable champions of pattern matching within strings. While both functions share the noble mission of pattern searching, they diverge in their behavior and method of execution. In this comprehensive and enlightening article, we embark on an illuminating journey, delving step-by-step into the intricacies of Python's re.search() and re.match() functions. With a few practical code examples and clear explanations, we aim to ... Read More

Advertisements