eLearning

We are a professional site that will give you the strongest methods to get learn and we will teach you everything about programming and technology

PYTHON

Breaking News

PYTHON
PYTHON
جاري التحميل ...

Texts in Python lesson 7

Texts in Python lesson 7



Text in Python is a string or array of characters. You can create texts in Python using single or double quotation marks:

"This is a string."

 'This is also a string.'

 v = "This is elearning"

 v = 'This is elearning blog'

Using the double quotation mark in creating texts in Python allows us to use the single quotation mark and vice versa as well without using the Escape character cancel code, and this is one of the advantages of Python as in the following example:

v = "This is elearning's Blog"

v = 'This is elearning' s Blog '

Text merge in Python

We can merge texts together in Python in more than one way, one of these methods is to use the + symbol, but it is preferable to avoid this method of merging texts:

Hello + world!

 => "Hello world!"

We can merge texts together without using the + symbol, in the following way:

Hello world!

=> "Hello world!"

We can also merge texts using the Format function, which is concerned with formatting texts, as will be explained below.

The text variable in Python is an array of characters. We can manipulate the text using indexing to access specific characters in the text:

"This is a string" [0]

=> 'T'

We use the auto-built len ​​function in Python to find the length of the text:

len ("This is a string")

 => 16

It is important to know in Python that the value None is an object:

None

=> None

To check the equality between a variable and the value of None we must use the method is in that and move away from using the expression == as follows:

"etc" isNone

=> False None isNone

 => True

None, 0, and null variables from strings/lists / dicts / tuples are all equal to the boolean False, the rest of the values ​​are True

Case in text

To convert English text to Titlecase, we use the title function with the text we want:

s = "e learning"

 print (s.title ())

 e learning

To convert English text into upper case or lower case, we use the upper and lower case functions respectively:

s = "elearning"

print (s.upper ())

> ELEARNING

print (s.lower ())

> elearning

If we want to reverse case, we use the swap case function, as in the following example:

s = "ElearNing"

print (s.swapcase ())

> eLEARnING

Search and replace in texts

Often times we need to search for a letter or group of characters in a text and replace it with another text or letter. We can perform these operations on texts in Python as follows:

To find out if there is a specific letter or text in another text, we can use the in operation in the following way:

s = "elearning"

print ('n' in s)

> True

 print ('elea' in s)

 > True

  print ('f' in s)

  False

  print ('x' in s)

  False

To find the index index for a character or text in another text, we use the index function, which returns the first index you find in the text:

s = 'elearning'

print (s.index ('l'))

> 2

print (s.index ('Q'))

> ValueError: substring not found


If the character or text is not present, the index function will return an error ValueError: substring not found, and to solve this problem we can use the find function which returns the value -1 in the absence of the text or the character we are looking for:


s = 'elearning'

print (s.find ('e'))

> 0

 print (s.find ('g'))

 > -1

In searching using the index function, we can specify the starting index at which the function starts searching and the ending index at which the function stops searching:

s = 'elearning'

print (s.index ('r', 4))

> 7

print (s.index ('r', 4,8))

> 7

To replace text, we use the replace function and pass to it the text we want to replace and the new text:

s = 'elearning'

print (s.replace ('e', 'the Ten'))

ethe Ten


عن الكاتب

ADMIN

التعليقات


اتصل بنا

If you like the content of our blog, we hope to stay in constant contact, just enter your email to subscribe to the blog express mail so that you will receive the new blog first-hand, and you can send a message by clicking the next button ...

جميع الحقوق محفوظة

eLearning