Python进阶(二):String类

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_37720172/article/details/78230025

Python进阶二

最近看《learn python the hard way》,作者喜欢鸡汤文啊。不过,生活便是如此,我们不可能一直都能激昂的热情去对待生活,偶尔读读鸡汤文也可以调剂一下生活,多一点期待和奋进,总比碌碌无为好吧。我会在文尾附上鸡汤原文,有兴趣的可以看一下。

进入正文…

string类

字符串类,没啥好说的,基本所有程序都有字符串类。这里主要讲讲他的一些骚操作。

1.利用 \* 例如 :这里写图片描述

结果:
这里写图片描述
这些还有:
这里写图片描述

2.格式化的字符串类,主要在字符串中插入中括号{}使用方法如下:

import random

i_want_to = "{}, I want to eat {}"

days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday',
        'Friday', 'Saturdat', 'Sunday']
fruits = ['banna', 'apple', 'watermelon', 'peach']


for i in range(4):
    rand = random.randint(0, 100)
    print(i_want_to.format(days[rand % 7], fruits[rand % 4]))

运行效果如下:
这里写图片描述
还有:

print("你多大了?", end=' ')# end=' ' 在结束这句话时,不会跳到下一行
age = input()#  在控制台输入的到字符串
print("我的年龄是:{}".format(age))

效果如图:
这里写图片描述
上面代码还可以简化为:

age = input("你多大了?")
print("我的年龄是:{}".format(age))

**今天就到这,讲得不多,希望你们会喜欢。
上文说过《learn python 3 the hard way》作者喜欢鸡汤,下面摘抄我看到的比较好的一段,希望能够一起加油下去吧。**

While you are studying programming, I’m studying how to play guitar. I
practice it every day for at least two hours a day. I play scales, chords, and
arpeggios for an hour and then learn music theory, ear training, songs, and
anything else I can. Some days I study guitar and music for eight hours
because I feel like it and it’s fun. To me repetitive practice is natural and just
how to learn something. I know that to get good at anything you have to
practice every day, even if I suck that day (which is often) or it’s difficult.
Keep trying, and eventually it’ll be easier and fun.
Between the time that I wrote Learn Python The Hard Way and Learn Ruby
The Hard Way I discovered drawing and painting. I fell in love with making
visual art at the age of 39 and have been spending every day studying it in
much the same way that I studied guitar, music, and programming. I collected
books of instructional material, did what the books said, painted every day,
and focused on enjoying the process of learning. I am by no means an
“artist,” or even that good, but I can now say that I can draw and paint. The
same method I’m teaching you in this book applied to my adventures in art. If
you break the problem down into small exercises and lessons, and do them
every day, you can learn to do almost anything. If you focus on slowly
improving and enjoying the learning process, then you will benefit no matter
how good you are at it.
As you study this book, and continue with programming, remember that
anything worth doing is difficult at first. Maybe you are the kind of person
who is afraid of failure, so you give up at the first sign of difficulty. Maybe
you never learned self-discipline, so you can’t do anything that’s “boring.”
Maybe you were told that you are “gifted,” so you never attempt anything
that might make you seem stupid or not a prodigy. Maybe you are
competitive and unfairly compare yourself to someone like me who’s been
programming for more than 20 years.
Whatever your reason for wanting to quit, keep at it. Force yourself. If you
run into a Study Drill you can’t do, or a lesson you just do not understand,
then skip it and come back to it later. Just keep going because with
programming there’s this very odd thing that happens. At first, you will not
understand anything. It’ll be weird, just like with learning any human
language. You will struggle with words and not know what symbols are
what, and it’ll all be very confusing. Then one day BANG—your brain will
snap and you will suddenly “get it.” If you keep doing the exercises and keep
trying to understand them, you will get it. You might not be a master coder,
but you will at least understand how programming works.
If you give up, you won’t ever reach this point. You will hit the first
confusing thing (which is everything at first) and then stop. If you keep
trying, keep typing it in, keep trying to understand it and reading about it, you
will eventually get it. If you go through this whole book, and you still do not
understand how to code, at least you gave it a shot. You can say you tried
your best and a little more, and it didn’t work out, but at least you tried. You
can be proud of that.

猜你喜欢

转载自blog.csdn.net/weixin_37720172/article/details/78230025
今日推荐