Python Classroom image

Anil  Bist / Professional / Web Technology

 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15|Last
Lessons:-INTRODUCTION

1.5. STRINGS, PART I 14
Finding remainders will prove more useful than you might think in the future!
1.5. Strings, Part I
Enough with numbers for a while. Strings of characters are another important type in Python.
1.5.1. String Delimiters, Part I. A string in Python is a sequence of characters. For Python to
recognize a sequence of characters, like hello, as a string, it must be enclosed in quotes to delimit the string.
For this whole section on strings, continue trying each set-off line of code in the Shell. Try
"hello"
Note that the interpreter gives back the string with single quotes. Python does not care what system you
use. Try
’Hi!’
Having the choice of delimiters can be handy.
Exercise 1.5.1.1. * Figure out how to give Python the string containing the text: I’m happy. Try it.
If you got an error, try it with another type of quotes, and figure out why that one works and not the first.
There are many variations on delimiting strings and embedding special symbols. We will consider more
ways later in Section 1.8.
A string can have any number of characters in it, including 0. The empty string is ’’ (two quote
characters with nothing between them).
Strings are a new Python type. Try
type(’dog’)
type(’7’)
type(7)
The last two lines show how easily you can get confused! Strings can include any characters, including digits.
Quotes turn even digits into strings. This will have consequences in the next section....
1.5.2. String Concatenation. Strings also have operation symbols. Try in the Shell (noting the space
after very):
’very ’ + ’hot’
The plus operation with strings means concatenate the strings. Python looks at the type of operands before
deciding what operation is associated with the +.
Think of the relation of addition and multiplication of integers, and then guess the meaning of
3*’very ’ + ’hot’
Were you right? The ability to repeat yourself easily can be handy.
Exercise 1.5.2.1. * Figure out a compact way to get Python to make the string, “YesYesYesYesYes”,
and try it. How about “MaybeMaybeMaybeYesYesYesYesYes” ? Hint: 2
Predict the following and then test. Remember the last section on types:
7+2
’7’+’2’
Python checks the types and interprets the plus symbol based on the type. Try
’7’+2
With mixed string and int types, Python sees an ambiguous expression, and does not guess which you want
– it just gives an error! 3
2Hint for the second one: use two *’s and a +.
3Be careful if you are a Java programmer! This is unlike Java, where the 2 would be automatically converted to ’2’ so the
concatenation would make sense.

 
 
 
image
Anil  Bist

Skills    Python

Qualifications :- High School - SLV, College/University - Graphic Era Deemed Univ University,
Location :-Dehradun,Dehradun,Uttarakhand,India
Description:-

I started my Professional Journey in 2006 with one of the Web Development Company in Bangalore and my 1st framework was "Ruby on Rail" as Web development and delivered around 5+ Projects using this platform. Then came another dimension as JEE/Sturst framework, Gradually I realized that I want to build something on my own and give my passion and energy on creating something different a
Explore

 

  Students (0)