So, assigning a string to a variable is just
like assigning a number to a variable. We can do
that with an assignment statement. My name is Dave. So,
I'll assign the string Dave, to the name, to the
variable name. The nice thing about using a variable like
this, is now we could have code. Say print Hello
plus name. Here hello is a string. Name is a
variable. If you assigned name to mean some other string,
this will print Hello whatever the value of
name is. I've introduced a new operator here.
We're using the plus operator. We already saw
plus on numbers. Now, we're using plus on strings.
It means something different. What plus means with
strings is concatenation. So we can put strings
together by using the plus operator. The value
of the string plus another string is the concatenation.
That's a new string that's a result of pasting
those two strings together. So let's try that in
the Python interpreter. So here I've initialized the variable
name to be the string Dave, and I'm going to
print out hello plus name. You'll see I won't
get quite what I want from this? Here's what
happens, it concatenated the two strings together, it doesn't
add spaces or anything automatically. So now we've got,
the output is hello Dave, without any space in
it. We need to add a space here. Now
we'll have hello space Dave, which looks more like
we want. We can keep doing this just like we
could use the plus operator with numbers more than
once, we can use it with strings more than once.
Adding another string to the end, now we've got
the exclamation point I need. Maybe one is not enough.
We can keep adding more exclamation points. Now I have a more pleasant reading.
変数に文字列を代入することは
変数に数値を代入するのと同様です
代入文を使って行うことができます
私の名前はDaveなので
文字列Daveを変数nameに代入します
このように変数を使用する利点は
コードにできることです
print ‘Hello’ + nameを実行するとします
このHelloは文字列でnameは変数です
別の文字列をnameに代入した場合は
Helloに続けて
nameの値になっているものが表示されます
ここで新たな演算子を挿入しました
+という演算子を使っています
数値の説明で+を紹介しましたが
今は文字列で+を使っています
文字列の場合では違う意味となります
文字列において+が意味するのは連結です
つまり+演算子を使うと文字列同士を連結できます
“文字列 + 文字列”は連結することを表します
2つの文字列をくっつけてできた
新たな文字列となります
これをPythonインタプリタで試してみましょう
変数nameに文字列Daveを設定しました
そして‘Hello’ + nameを表示します
求める形と完全に同じにならないことが分かりますか?
これが結果です 2つの文字列が連結しました
スペースなどは自動的に追加されません
そのためスペースなしでHelloDaveと出力されたのです
ここにスペースを加える必要があります
これでHello Daveになります
求める形に近づきました
これを続けることができます
数値の時に+演算子を何度も使用できましたが
文字列でも何度も使用することができます
最後に別の文字列を加えると
このように感嘆符もつけられます
1つでは足りませんので
感嘆符をさらに追加していくことができます
これでもっと楽しく読めるようになりました
Karakter dizisini bir değişkene atamak
bir sayıyı
değişkene atamakla aynıdır. Bu işlemi,
atama
operatörünü kullanıp gerçekleştirebiliriz.
Benim ismim Dave. O yüzden kendi
ismimi isim değişkenine atıyorum. Değişken
kullanmanın faydası bu değişkeni kodumuzda
rahatça kullanabilmektir. Mesela basitçe
print 'Hello' + name şeklinde bir kod
yazabiliriz. Burada Hello bir karakter
dizisidir. Name ise
değişkendir.Değişkenin değerini daha sonra
değiştirirseniz bu kez o
isme merhaba diyecektir. Burada yeni
bir operatör kullandım. Bildiğimiz +
operatörü. Daha önce rakamlar için
kullanmıştık,
şimdi karakter dizileri için kullanıyoruz.
Yani biraz daha farklı bir kullanımı var.
Karakter dizilerinde + bir araya getirme
anlamını taşır.
Yani iki karakter dizisini + operatörü ile
bir araya
getirebiliriz. Karakter dizisi + diğer
karakter dizisinin değeri
birleştirilen yeni string olacaktır. İki
string'i birleştirerek yeni bir değer elde
ettik. Şimdi bunu Python interpreter'da
görelim. Burada değişkenimi Dave ismine
atıyorum. Merhaba İsim(değişken)
çıktısını yazdıracağım. Gördüğünüz gibi
istediğimi tam olarak yapamadım, değil mi?
Bunun sebebi yorumlayının iki stringi
birleştirirken ortasına herhangi bir ara
veya boşluk bırakmaması
tam olarak birleştirmesidir. Dolayısıyla
çıktı olarak hello Dave'i boşluksuz verdi.
Buraya bir boşluk eklemeliyiz. Şimdi
istediğimiz sonuç olan
hello boşluk Dave'i göreceğiz.
Artı operatörünü birden fazla rakamı
toplamak için
kullanabildiğimiz gibi birden fazla
karakter dizisini de bir
araya getirmek için kullanabiliriz.
Bu ifadenin sonuna başka bir
string, "!" getirelim. Hatta birden fazla
ünlem
işareti olsun. İstediğimiz kadar
koyabiliriz.
Şimdi çok daha güzel bir cümle oluşturmuş
oldum.
将字符串赋值给变量就如同将数字赋值给变量一样
我们可以通过赋值语句来实现这项功能
我的名字是 Dave
所以我要把 Dave 字符串赋值给变量 name
像这样使用变量的好处是 现在我们可以创建代码
例如输出 Hello 加上 name
这里的 Hello 是一个字符串
Name 是一个变量 如果把 name 指代其他的字符串
那么就会输出 Hello 以及 name 所代表的任何值
刚刚我介绍了一个新运算符
我们使用了加号运算符
我们知道数字之间会用加号 现在我们在字符串之间使用加号
其含义有所不同
带字符串的加号表示连接
因此我们可以通过使用加号运算符将字符串放在一起
一个字符串加上另一个字符串的意思是连接
这是一个新的字符串 是将两个字符串放在一起得到的结果
我们在 Python 解释器中运行一下
我已经初始化了变量 name 使其成为字符串 Dave
我要输出 hello 加上 name
运行之后 大家可以看到
并没有得出我想要的结果
以下是运行之后得到的结果 两个字符串连在一起
彼此之间并没有自动添加空格或任何东西
因此我们得到的是 helloDave 中间没有空格
我们需要在这里加一个空格
这样我们得到了 hello Dave
它看起来更像是我们想要的结果
就像我们可以多次使用加号进行数字运算一样 对字符串也是如此
我们可以多次使用加号连接更多字符串
现在我们在字符串末尾加上另一个字符串
运行之后显示了我想要的叹号 可能一个不够
我们可以在字符串后面添加更多的叹号 这样读起来比较顺眼