strings
play

Strings C-START Python PD Workshop C-START Python PD Workshop - PowerPoint PPT Presentation

Strings C-START Python PD Workshop C-START Python PD Workshop Strings Special Characters \t C-START Python PD Workshop "Glow in the Dark" Favorite Color: print("Favorite Color: \n\t\" Glow in the Dark \" ")


  1. Strings C-START Python PD Workshop C-START Python PD Workshop Strings

  2. Special Characters \t C-START Python PD Workshop "Glow in the Dark" Favorite Color: print("Favorite Color: \n\t\" Glow in the Dark \" ") Here is an example of using some escape sequences: Horizontal Tab Newline Special characters can be inserted in a string using an escape \n Backslash \\ Double Quote \" some common escape sequences: sequence : a backslash ( \ ) followed by another character. Here are Strings

  3. Special Characters \t C-START Python PD Workshop "Glow in the Dark" Favorite Color: print("Favorite Color: \n\t\" Glow in the Dark \" ") Here is an example of using some escape sequences: Horizontal Tab Newline Special characters can be inserted in a string using an escape \n Backslash \\ Double Quote \" some common escape sequences: sequence : a backslash ( \ ) followed by another character. Here are Strings

  4. Single or Double Quotes: Your Choice Strings can be written using either single or double quotes, your choice. primary = 'Python' secondary = "English" Using single quotes means no need to escape double quotes: print('So you must be "the one"?') Using double quotes means no need to escape single quotes: print("Margaret's house is blue.") C-START Python PD Workshop Strings

  5. Single or Double Quotes: Your Choice Strings can be written using either single or double quotes, your choice. primary = 'Python' secondary = "English" Using single quotes means no need to escape double quotes: print('So you must be "the one"?') Using double quotes means no need to escape single quotes: print("Margaret's house is blue.") C-START Python PD Workshop Strings

  6. Single or Double Quotes: Your Choice Strings can be written using either single or double quotes, your choice. primary = 'Python' secondary = "English" Using single quotes means no need to escape double quotes: print('So you must be "the one"?') Using double quotes means no need to escape single quotes: print("Margaret's house is blue.") C-START Python PD Workshop Strings

  7. But unlike lists, strings cannot be modifjed: Strings Are Like Lists Strings are like lists containing characters: myname = "Jack" print(myname[0]) J myname = "Jack" myname[0] = "T" # bad C-START Python PD Workshop Strings

  8. Strings Are Like Lists Strings are like lists containing characters: myname = "Jack" print(myname[0]) J myname = "Jack" myname[0] = "T" # bad C-START Python PD Workshop Strings But unlike lists, strings cannot be modifjed:

  9. Strings are Iterables! for c in 'hello world': print (c) h e l l o w o r l d C-START Python PD Workshop Strings

  10. .split() ting Strings To separate a string into a list based on white spaces, call .split() on it. Here is an example: my_str = " Python is really cool" wordlist = my_str.split() # wordlist will be ["Python", "is", ... ] for word in wordlist: print (word) Python is really cool C-START Python PD Workshop Strings

  11. .split() ting Strings print (word) C-START Python PD Workshop on the object. allows us to use a function which is specifjc to a certain data type however, most programmers simply call it the dot operator . It The . operator used above is actually the accessor operator , for word in wordlist: To separate a string into a list based on white spaces, call # wordlist will be ["Python", "is", ... ] wordlist = my_str.split() really cool" Python is my_str = " .split() on it. Here is an example: Strings The . Operator

  12. Splitting the Input Remember that the input function returns a string contaning the line that the user typed. If we want to accept multiple words per line, we must split the input. line = input("What is your full name? ") Useful for Kattis Some Kattis problems require that you recieve input on a single line separated by spaces. This is an efgective method to receive the input. C-START Python PD Workshop Strings words = line.split() firstname = words[0] lastname = words[1]

  13. Splitting the Input Remember that the input function returns a string contaning the line that the user typed. If we want to accept multiple words per line, we must split the input. line = input("What is your full name? ") Useful for Kattis Some Kattis problems require that you recieve input on a single line separated by spaces. This is an efgective method to receive the input. C-START Python PD Workshop Strings words = line.split() firstname = words[0] lastname = words[1]

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend