Skip to main content

Basic Knowledge of Python part 2

Strings using python -

Other than numbers python can also handle strings which can be expressed by several ways. In programming a string is a contiguous sequence of symbols or values such as a character string or digit string. Strings is declared in a '....' (single quotes) or "...." (double quotes). 

  

By using '\n' you can insert the string to the new line by using print() command. For example:


 We can concatenate string by using + operator. Concatenate means we can merge two or more separate strings by using + operator. For example:


We see another example of multiple concatenate.


Here 3*'py' means the string 'py' repeats 3 times and same for the 'thon' string.

Now we another method to concatenate two or more strings. In this method you have write strings next to each other without any operator. For example:


In the python string is stored in indexes. The index is start from 0 (zero). In python indexes also be calculated from reverse side and this index is start from -1. Lets see example

str = 'Python'

Indexes
 0
 P
 -6 -5 -4-3  -2-1 
Indexes

With the help of indexing you can separate out the string means that you can get some part of main string using indexes. For example:


Related to the indexing now we see the slicing. In slicing you get a substring of the main string instead of getting single character. If we want 'tho' substring from 'Python' main string we use slicing. For these please refer above indexing table. 

 

Here value before colon is the starting index and value after colon is ending index so that you can get the values between these two indexes. 

We see some more examples on slicing.

If you want first two and last two characters from main string you should write this code:



We will learn more about strings in the different section.

Prev Topic  Next Topic 

Comments

Popular posts from this blog

Basic Knowledge of Python part 1

Basic Mathematics equations using Python- As we know python is a simple and easy to learn programming language. In this section we learn how to done mathematics equations using python. To perform this we use our basic operators +,-,/,* .  To perform python we have to open python interpreter. In this interpreter you can type simple equation like 5+5, 4/2 etc. For example: The integer number (e.g. 1,2,3....) have type int and the number with fractional part (e.g. 2.5,2.1,4.5...) have type float. Note:-  The division type always returns float. To get only an integer result of division you can use the // operator and to get remainder from division result you can use % operator. In python you can easily calculate power of the number by using ** operator. Like this you can easily calculate mathematics equation. Now we see how to find area of rectangle. Area = length*width is the formula to find area of rectangle. Here '=' sign is used to store value to the variable. You can als...

Python | Data Structure or Data Types (String)

Data Type Of Python I n the last session we discus about what is the data type of python. And we see List data type. In this session we s ee the next data type String.     2) String- String is a ordered collection of immutable data type. String are iterable and hashable. Strings are always declared in single or double quotes.  Single line string:- S = "Python" Multi line string:- S = """ Line 1             Line 2             Line 3 """ Iterable:- S= "Program" for i in S:       print(i) Output:-  P  r  o  g  r  a  m Membership:-  print( 'p' in S) => True print( 'P' not in S) => False Methods of string:- capitalize() :-  It return the capitalize version of the string. More specifically it make the first character have uppercase and rest lowercase. For example:- S = "hello World" print(S.capitalize()) => "Hello world" count(substring, start,en...

Python | Data Structure Or Data Type (Tuple)

Data Type Of Python  In this session we will see last data type of python which is Tuple. 5) Tuple:- Tuple is a ordered collection of immutable datatype, tuples are quite similar to lists but difference is list are mutable where as tuple are immutable. Empty tuple:-  t = () or tuple() Tuple with single element:- t = (10,) Tuple with multiple element:- t1 = ( 10, 20, 30, 'Hello' ) Nested tuple:-  t1 = ( 10, 20, 30, 'Hello' ) p = t2, 30, 40, 50 print(p) => ((10, 20, 30, 'Hello'), 30, 40, 50) Methods of Tuple:-   count(value):- It return the count of value if the value is present in the tuple else it return 0. For example:- t = (10, 20, 30, 'Hello') t.count(10) => 1  t.count(50) => 0 index(value,start,end):- It return the index number if the value is present in the tuple, otherwise it raised value error. For example:- t = (10, 20, 30, 'Hello') t.index(10) => 0 It return the index number of value 10. Prev Topic   Next Topic