2015年2月10日 星期二

Python ─ Strings & Console Output

Strings and Console Output


※ 宣告brian = 字串

brian = "Hello life";  
_________________________________________

※ 宣告並列印值

caesar = "Graham";
praline = "John";
viking = "Teresa";

# print value

print caesar
print praline
print viking

---------------------------------------------------------
Result:Graham
            John
            Teresa
_________________________________________

※ 若字串要印出單引號( ' )  前面要加 \

'This isn't flying, this is falling with style!'

應修改為:       This isn\'t flying, this is falling with style!'

_________________________________________

※ 宣告 fifth_letter 為 MONTY 並以陣列的方式印出 " Y "
+---+---+---+---+---+
| M | O | N | T | Y | 
+---+---+---+---+---+
   0    1    2    3    4  

fifth_letter = "MONTY"[4]; 

print fifth_letter;

---------------------------------------------------------
Result: Y
_________________________________________

●  字串的方法 (String methods)

○ len( 變數名稱)

parrot="Norwegian Blue";         # 宣告parrot為字串

len(parrot);                            # len(變數)→ 長度

print len(parrot);                    # 印字串長度 

---------------------------------------------------------
Result:14
_________________________________________

○ 變數名稱.lower()          # 把字全部印成小寫


parrot = "Norwegian Blue"

parrot.lower();                          # 將parrot的值全部改成小寫

print parrot.lower();                  # 印出parrot.lower()

---------------------------------------------------------
Result: norwegian blue
_________________________________________

○ 變數名稱.upper()          # 把字全部印成大寫

parrot = "norwegian blue"

parrot.upper();

print parrot.upper();

---------------------------------------------------------
Result: NORWEGIAN BLUE
_________________________________________

○ str(變數名稱)         # 將不是字串的值的型態  印成字串型態

pi =3.14;                  # 宣告pi為double型態

str(pi);                       

print str(pi);

---------------------------------------------------------
Result: 3.14  ───> 以轉換為字串型態
_________________________________________

※ 呼叫方法用 "."

ministry = "The Ministry of Silly Walks"   # 宣告ministry 為字串

 # 可省略 len(ministry)、  ministry.upper()不寫直接印

print len(ministry);                             

print ministry.upper();         # 用 "." 呼叫upper()

---------------------------------------------------------
Result: 27
             THE MINISTRY OF SILLY WALKS
_________________________________________

※ Printing Strings   直接印出字串

print "Monty Python"        # 直接print字串,不用另外宣告

---------------------------------------------------------
Result: Monty Python
_________________________________________

※ Printing Variables   印出字串的值

the_machine_goes ="Ping!";

print the_machine_goes;          # 印出the_machine_goes的值

---------------------------------------------------------
Result: Ping
_________________________________________

※ String Concatenation  字串相加

print "Spam "+"and "+"eggs";

---------------------------------------------------------
Result: Spam and eggs
_________________________________________


Explicit String Conversion  印出字串,並將非字串型態的值轉換為字串

print "The value of pi is around " + str(3.14);            # str() 將裡面的值轉為字串

---------------------------------------------------------
Result: "The value of pi is around 3.14  
_________________________________________

※ String Formatting with %, Part 1   宣告n個字串並以(%s)做代表依序將字串印出

string_1 = "Camelot"            # 宣告字串1

string_2 = "place"                # 宣告字串2


print "Let's not go to %s. 'Tis a silly %s." % (string_1, string_2)    #  記得加上% (變數名稱1、變數名稱2....)

---------------------------------------------------------
Result:"Let's not go to "Camelot. 'Tis a sillyplace.
_________________________________________

※ String Formatting with %, Part 2  建立一個互動式問答並印出結果

(原始檔)
name = raw_input("What is your name?")                    # 設定一個會自動顯示在console的input字串
quest = raw_input("What is your quest?")                    # 設定一個會自動顯示在console的input字串
color = raw_input("What is your favorite color?")        # 設定一個會自動顯示在console的input字串

print "Ah, so your name is __, your quest is __, "           # 印出字串
"and your favorite color is __."


(修改為)
name = raw_input("What is your name?")
quest = raw_input("What is your quest?")
color = raw_input("What is your favorite color?")

print "Ah, so your name is \%s, your quest is \%s,, "               # 將 input字串依序帶入

print "and your favorite color is \%s,."                                  # 需要再加一個print 才能印出來

---------------------------------------------------------
Result:Ah, so your name is \%s, your quest is \%s,,
            and your favorite color is \%s,.
_________________________________________

綜合練習

my_string ="Hello";            # 宣告一個字串
print len(my_string);         # 印出字串的長度
print my_string.upper();    # 將字串的值全部以大寫印出


沒有留言:

張貼留言