String and number in python

Posted on 8 June, 2019 by Administrator

String and number in python

1. String data type

All programming languages have a data type of string type, and in Python it is no exception. In this article, I will show how to use string and special string in python

a. Special character in python

To use special characters in Python when printing data, we use \ before it.

For example you want to print double quote " but you use " "to contain the string you want to print

>>>print("hello \"\" ")
hello ""

And we can do with single quote '

>>>print('hello \'\' ')

hello ''

Other special characters:

  • \n break down the line and start the new line

  • \t push the content behind a tab

  • \a escaped character sequence for control character BEL

  • \b remove the space ahead


b. String binding python

In addition to the ways of printing the data above, we can also use keyword formats for the value type and bind it to the string. Use with syntax:

print("%type" %(binding))

type is the data type you want to bind and replace it with.

binding is the value you want to bind to the position specified in the string.


Below here is the following types:

  • %c character

  • %s string

  • %i integer

  • %d integer

  • %o octal

  • %x hexadecimal (normal print)

  • %X hexadecimal (in capital letters)

  • %e exponent (with normal)

  • %E exponent (with capital)

  • %f real number


For example i want to binding a string into a string

>>>food = "burger"
>>>shop = "Burger King"
>>>result = "I love %s and I buy it in %s" %(food,shop)
I love burger and I buy it in Burger King

if you want to bind multiple strings into the string, each value you want to bind is separated by a comma

And you can binding string, number into another string like this

>>>day = 1
>>>month = 1
>>>year = 2019
>>>blog = ""
>>>result = "Today - %i/%i/%i i learn python from blog %s" %(day,month,year,blog)
Today - 1/1/2019 i learn python from blog

You can try it here

c. Access to each value of the string.

The string in Python is stored in array cells with each cell equivalent to a single character (different from other languages) and these characters are sequentially arranged together. Therefore, the string data type in Python can be accessed to each character in it (and in PHP, only PHP7 supported as reverse index string).

To access each character inside the string, you use the following syntax:

  • stringName is the name of the variable containing the string, or string.

  • index is the position of the character you want to retrieve. This index supports us to retrieve both directions of the string if:

    • From the beginning, it starts at 0

    • From the end, it starts at -1
      String and number in python

For example:

>>quote = "devpython"
>>print(quote[0]) # d
>>print(quote[-1]) # n

If you want to get the contents of a string segment in that string, you can use the following syntax:

  • stringName is the name of the variable containing the string, or string.

  • start is the position of the start character, if left blank start it will be taken from 0.

  • end is the end position (it will be taken from start to <end), if left blank, it will get to the end of the string.

For example:

quote = "devpython"
print(quote[0:3]) # dev
print(quote[-6:9]) # python
print(quote[:3]) # dev
print(quote[3:]) # python

Try it here

2. Number data type

A variable declared as a numeric data type in Python, in terms of memory storage, it will not change the value, but when we assign a new value to that variable, it will actually create cells. Another new memory to store that new value.

In python supports us with 3 types of numeric data as follows:

  • int – intertype type, this type has unlimited size (but python 2 is limited).

  • float real number type This type, in addition to the normal writing, can also be displayed as E exponent (eg 2.5e2 = 250).

  • complex this complex number complex is a very rarely used data type, so I will discuss this type in later article

If you want to free a memory for a variable in Python, you can use the del command with the following syntax:

>>>del variableName
#del mutli variable
>>>del variableName1, variableName2

variableName1, variableName2, ... are the variables you want to release.

animal = "dog"
print(animal) # dog
del animal
# 'animal' is not defined

You can try it here

3. Conversion data type in python

In a case where you want to convert the data type of a variable, Python also supports you through the following basic functions:

  • float(data) convert to real number type

  • int(data, base) convert to numeric type, where base is the type of factor you want to convert to (this parameter can be left blank).

  • str(data) convert to a string type

  • complex(data) convert to a complex type

  • tuple(data) convert to tuple type

  • dict(data) convert to dictionary type

  • hex(data) convert to hexa type

  • oct(data) convert to octa type

  • chr(data) convert to character type

For example:

>>>count = 100
>>>toFloat = float(count)
<class 'float'>
>>>toInt = int(count)
<class 'int'>
>>>toStr = str(count)
<class 'str'>

You can try it here

Tags: python3, python


New blog posts

String and number in python
String and number in python

8 June, 2019 by Administrator

1. String data type All programming...

Variables expressions and statements in python
Variables expressions and statements in python

2 June, 2019 by Administrator

1. Value and type Value is one of...

First python hello world
First python hello world

31 May, 2019 by Administrator

1. What is Python: Python is an Interpreter...

View all blog entries →

Give away $50 Digital Ocean