Saturday, December 10, 2016

Python String


Today is my third tutorial. Today I am discussing String only

string. So, Let’s begin our string journey. The string is a big 

Topic. This is our first String Class.

STRING



What is String?

A – Z all are characters. We make words and writes sentence using

those characters. A string is a sequence of characters. Example :

joy, Mother, Father and so on. String are like the sentence. String

are very important when you communicating to the user. Keep remember

when using string your program always use quote I.e: “This is a

string.” You can use single quote also. Like ‘I am joy’.




Ok,ok Now we are focusing on Python string. Python string are very

easy but you have to remember some rules and methods. If you

remember those methods and rules, your life will be easy.


Always remember that python strings are immutable.




What the hack? What is immutable? I never heard that?

Once python string created , you can not change the character.
That is immutable. Nothing fancy. Now let’s get coding.



# declare a variable.
# assign a string in this variable.
# use the type() to check the variable type.



>>> country = “ Bangladesh”
>>> type(country)
<type ‘str’>



See that country variable type is the string. You can also print this
variable.
>>> print country
We can also single quotation.
>>> city=’Dhaka’
>>> city



















If you are genius or brilliant like me , then use this example and
see what happened?
>>> country = ‘usa”













see that, I start single quotation and finish double quotation. Very
good. Now you encounter an error.
Error name is SyntaxError: EOL while scanning string literal.

EOL means End of line. This means program found end of line but
doesn’t found string.
Now, you tell me that I want to use single quotation on my string.

>>> name = “Adam’s “
>>> name
Adam’s”
You can also write this.
>>> name = “Adam\’s”
>>> name


















I use backward slash (\) before the single quote.



Accessing Values in Strings

I give you a string. Now you ask me that how I can access string
character? Can I access string character?

My answer is YES. You access string character. But how?. If you want
to access string character, you have to use this standard [] syntax.
But I want to tell you something important very important which is
Indexing.

What is Indexing?
The index is such kind of method where you put numeric values and it
gives you a value. When you use indexing you will have to use []
notation. Let’s coding. Write all code.

Tips: If you don’t write code, you don’t learn anything.


Index number always start with zero.
>>> country = ‘Bangladesh’
Here, Bangladesh is a 10 character. You want to access B.
B a n g l a d e s h
0 1 2 3 4 5 6 7 8 9


>>> country [0]
>>> ‘B’
>>> country [1]
a’












You can access one character like this.
You can also measure string length. There is a built-in function
len(). len() is a built-in function which tells your string length.



>>> country = 'United States Of America’
>>> len(country)












Here space is also character. len() counts space as a character.
length shows 10. Country last index is = len(Country)-1=9


>>> Country[10]
IndexError: string index out of range.




Country[10] give me an IndexError. That means my index range is 0 t0
9. But I give 10. That’s why index error happened.


RED-ALERT String immutable

Immutable means you don’t change your string character. Example:


>>> name = “ Albert”
>>> name[0] = “a”
TypeError: ‘str’ object does not support item assignment



















That means if you already created a string, you can’t change the
string. That means immutable or Non-mutable.

That’s for today. The string is a big topic. We will discuss next Topic.

Be Safe, make other people happy and Be happy.


Important link:

https://developers.google.com/edu/python/strings
tutorials point
Basic String
learn python the hard way
http://www.pythonforbeginners.com/basics/string-manipulation-in-python 

https://www.programiz.com/python-programming/string



No comments:

Post a Comment