Strings and Serialization Damian Gordon STRING MANIPULATION Strings
- Slides: 31
Strings and Serialization Damian Gordon
STRING MANIPULATION
Strings and Serialization • Some basics: a = "Hello" b = 'World' c = '''a multiple line string''' d = """More Multiple Strings""" e = ("Three " "Strings " "Together")
Strings and Serialization • Some basics: a = "Hello" b = 'World' c = '''a multiple line string''' d = """More Multiple Strings""" e = ("Three " "Strings " "Together") Hello World a multiple line string More Multiple Strings Three Strings Together
Strings and Serialization • Some methods: s = "Hello World“ s. count('o‘) # How often does ‘o’ appear in s? s. count(‘l‘) # How often does ‘l’ appear in s? s. find('o‘) s. find(‘l‘) # What position is the first ‘o’ at? # What position is the first ‘l’ at? s. rfind('o‘) s. rfind(‘l‘) # What position is the last ‘o’ at? # What position is the last ‘l’ at?
Strings and Serialization • Some methods: s = "Hello World“ s. count('o‘) # How often does ‘o’ appear in s? s. count(‘l‘) # How often does ‘l’ appear in s? 2 # two o’s 3 # two l’s s. find('o‘) # What position is the firstat‘o’ at? 4 # starting zero s. find(‘l‘) # What position is the firstat‘l’ at? 2 # starting zero s. rfind('o‘) s. rfind(‘l‘) 7 # starting at zero # What position is the last ‘o’ at? 9 # starting at zero # What position is the last ‘l’ at?
Strings and Serialization • Some methods: s = "Hello World, how are you? “ s 2 = s. split(' ') # create an array split by spaces s 3 = '#'. join(s 2) # join the above array with #’s s 4 = s. replace(' ', '**') # replace spaces with stars s 5 = s. partition(' ') # split based on first space s 6 = s. rpartition(' ') # split based on last space
Strings and Serialization • Some methods: s = "Hello World, how are you? “ s 2 = s. split(' ') # create an array split by spaces ['Hello', 'World, ', 'how', 'are', 'you? '] s 3 = '#'. join(s 2) # join the above array with #’s Hello#World, #how#are#you? s 4 = s. replace(' ', '**') # replace spaces with stars Hello**World, **how**are**you? ' ', s 5 = s. partition(' ') # ('Hello', split based World, s 6 = s. rpartition(' ') # ('Hello split based 'World, howspace are you? ') on first how are', space ' ', 'you? ') on last
String Formatting
Strings and Serialization • String formatting: Standard. Text = "Hello {}, you are currently {}" print(Standard. Text. format('Damian', 'asleep'))
Strings and Serialization • String formatting: Standard. Text = "Hello {}, you are currently {}" print(Standard. Text. format('Damian', 'asleep')) Hello Damian, you are currently asleep
Strings and Serialization • String formatting: Standard. Text = "Hello {}, you are currently {}" print(Standard. Text. format('Damian', 'asleep')) Hello Damian, you are currently asleep Standard. Text = "Hello {0}, you are currently {1}. Your name is {0}" print(Standard. Text. format('Damian', 'asleep'))
Strings and Serialization • String formatting: Standard. Text = "Hello {}, you are currently {}" print(Standard. Text. format('Damian', 'asleep')) Hello Damian, you are currently asleep Standard. Text = "Hello {0}, you are currently {1}. Your name is {0}" print(Standard. Text. format('Damian', 'asleep')) Hello Damian, you are currently asleep. Your name is Damian
Strings and Serialization • String formatting: Standard. Text = "Hello {}, you are currently {}" print(Standard. Text. format('Damian', 'asleep')) Unindexed Arguments Hello Damian, you are currently asleep Standard. Text = "Hello {0}, you are currently {1}. Your name is. Indexed {0}" Arguments print(Standard. Text. format('Damian', 'asleep')) Hello Damian, you are currently asleep. Your name is Damian
Strings and Serialization • String formatting (using indexed arguments): template = """ public class {0} {{ public static void main(String[] args) {{ System. out. println("{1}"); }} }}""“ print(template. format("My. Class", "print('hello world')"));
Strings and Serialization • String formatting (using indexed arguments): A string over multiple lines template = “““ public class {0} {{ public static void main(String[] args) {{ System. out. println("{1}"); }} The. format() method will replace all ‘{{‘ with a ‘{‘ and all ‘}}’ with a single ‘}’ }}””” print(template. format("My. Class", "print('hello world')")); Replace {0} with the first parameter, and {1} with the second one
Strings and Serialization • String formatting (using indexed arguments): template = “““ public class {{ My. Class { public{0} class public static void main(String[] args) {{ public static void main(String[] args) { System. out. println("{1}"); }} System. out. println("print('hello world')"); }}””” } print(template. format("My. Class", "print('hello world')")); }
Strings and Serialization • String formatting with parameter names (keyword arguments): template = """ From: <{from_email}> To: <{to_email}> Subject: {subject} {message}""" print(template. format( from_email = "Damian. Gordon@dit. ie", to_email = "You@student. dit. ie", message = "Here's some mail for you. " " Hope you enjoy the message!", subject = "You have mail!" ))
Strings and Serialization • String formatting with parameter names (keyword arguments): template = """ From: <{from_email}> To: <{to_email}> Subject: {subject} {message}""" From: <Damian. Gordon@dit. ie> To: <You@student. dit. ie> Subject: You have mail! print(template. format( Here's some mail for you. from_email = "Damian. Gordon@dit. ie", you enjoy the message! to_email = "You@student. dit. ie", message = "Here's some mail for you. " " Hope you enjoy the message!", subject = "You have mail!" )) Hope
Strings and Serialization • String formatting with a mix of unindexed and keyword arguments: print("{} {label} {}". format("x", "x + 1", label = "="))
Strings and Serialization • String formatting with a mix of unindexed and keyword arguments: print("{} {label} {}". format("x", "x + 1", label = "="))
Strings and Serialization • String formatting with a mix of unindexed and keyword arguments: print("{} {label} {}". format("x", "x + 1", label = "=")) x = x + 1
Strings and Serialization • String formatting with container lookups: emails = ("Damian. Gordon@dit. ie", "You@student. dit. ie") message = { 'subject': "You Have Mail!", 'message': "Here's some mail for you!" } template = """ From: <{0[0]}> To: <{0[1]}> Subject: {message[subject]} {message[message]}""" print(template. format(emails, message=message))
Strings and Serialization • String formatting with container lookups: emails = ("Damian. Gordon@dit. ie", "You@student. dit. ie") message = { 'subject': "You Have Mail!", 'message': "Here's some mail for you!" } template = """ From: <{0[0]}> To: <{0[1]}> Subject: {message[subject]} {message[message]}""" print(template. format(emails, message=message))
Strings and Serialization • String formatting with container lookups: emails = ("Damian. Gordon@dit. ie", "You@student. dit. ie") message = { 'subject': "You Have Mail!", 'message': "Here's some mail for you!" } template = """ From: <{0[0]}> To: <{0[1]}> Subject: {message[subject]} {message[message]}""" print(template. format(emails, message=message))
Strings and Serialization • String formatting with container lookups: emails = ("Damian. Gordon@dit. ie", "You@student. dit. ie") message = { 'subject': "You Have Mail!", 'message': "Here's some mail for you!" } template = """ From: <{0[0]}> To: <{0[1]}> 1[subject] Subject: {message[subject]} {message[message]}""" 1[message] message print(template. format(emails, message=message))
Strings and Serialization • String formatting with container lookups: emails = ("Damian. Gordon@dit. ie", "You@student. dit. ie") message = { 'subject': "You Have Mail!", 'message': "Here's some mail for you!" From: <Damian. Gordon@dit. ie> } To: <You@student. dit. ie> template = """ From: <{0[0]}> Subject: You Have Mail! To: <{0[1]}> some mail for you! Subject: Here's {message[subject]} {message[message]}""" print(template. format(emails, message=message))
Strings and Serialization • String formatting with classes: class EMail: def __init__(self, from_addr, to_addr, subject, message): self. from_addr self. to_addr = self. subject = self. message = # END init # END class. = from_addr to_addr subject message Continued
Continued Strings and Serialization • String formatting with classes: email = EMail(“Damian. Gordon@dit. ie", “You@student. dit. ie", "You Have Mail!", "Here's some mail for you!") template = """ From: <{0. from_addr}> To: <{0. to_addr}> Subject: {0. subject} {0. message}""" print(template. format(email))
Strings and Serialization • String formatting with classes: From: <Damian. Gordon@dit. ie> To: <You@student. dit. ie> Subject: You Have Mail! Here's some mail for you!
etc.
- Licenseid=string&content=string&/paramsxml=string
- Damian gordon
- Index.of.secret
- Damian gordon
- Performing file i/o using hdfs
- Visual basic string
- String manipulation
- String manipulation in java
- C string manipulation
- String manipulation
- Sncm serialization
- What is the use of serialization
- Kotlinx serialization optional
- Network serialization
- Jws json serialization
- Drug supply chain security act definition
- Kotlinx serialization performance
- Char char slide
- Private string name;
- Str string
- Damian sturzaker
- Padre damian de veuster 2215
- Ejemplo de biografía de una persona
- Padre damian de veuster 2215 vitacura
- Damian clancy
- Damian krysztofik
- Dr damian folch
- Damian urbańczyk partnerka
- Christophe damian
- Damian mac
- What did fray damian massanet do
- Padre damian de veuster 2215 vitacura