Welcome Turk Hack Team Members,
join()
The join(sequence) method joins elements and returns the combined string. The join methods combines every element of the sequence.
Combine list of words?
Combine them into a sentence with the join(sequence) method. The method is called on a seperator string, which can be anything from a space to a dash.
This is easier than using the plus operator for every word, which would quickly become tedious with a long list of words.
Example
The join method takes a sequence as argument. The sequence is written as single argument: you need to add brackets around the sequence.
If youd like, you can pass a variable holding the sequence as argument. This makes it easier to read. Well use a space a seperator string in the example below.
Try the program below:
You should see this output:
It can also join a list of words:
If you are a beginner, then I highly recommend this book.
Exercise
replace()
Python has builtin support for string replacement. A string is a variable that contains text data. If you dont know about strings, you can read more about strings in this article.
Can call the string.replace(old, new) method using the string object. This article demonstrates the replace method.
Not all programming languages have a standard string replace function. Python has a lot of functionality that comes out of the box.
Example
Replace method
Define a string and call the replace() method. The first parameter is the word to search, the second parameter specifies the new value.
The output needs to be saved in the string. If you dont save the output, the string variable will contain the same contents. Saving output is done using: s = function()
Try the program below:
Save the program as app.py, then run in terminal (or IDE)
python app.py
This will output the new output of string variable s:
Number of words to replace
An optional parameter is the number of items that will be replaced. By default its all.
The program below replaces only the first item:
The parameter (1) indicates that the string should be replaced only once.
Exercise
Try the replace program
Can a string be replaced twice?
Does replace only work with words or also phrases?
//Quoted. Thanks for reading.
join()
The join(sequence) method joins elements and returns the combined string. The join methods combines every element of the sequence.
Combine list of words?
Combine them into a sentence with the join(sequence) method. The method is called on a seperator string, which can be anything from a space to a dash.
This is easier than using the plus operator for every word, which would quickly become tedious with a long list of words.
Example
The join method takes a sequence as argument. The sequence is written as single argument: you need to add brackets around the sequence.
If youd like, you can pass a variable holding the sequence as argument. This makes it easier to read. Well use a space a seperator string in the example below.
Try the program below:
Kod:
[COLOR="PaleGreen"]# define strings
firstname = "Bugs"
lastname = "Bunny"
# define our sequence
sequence = (firstname,lastname)
# join into new string
name = " ".join(sequence)
print(name)[/COLOR]
You should see this output:
It can also join a list of words:
Kod:
[COLOR="palegreen"]words = ["How","are","you","doing","?"]
sentence = ' '.join(words)
print(sentence)[/COLOR]
If you are a beginner, then I highly recommend this book.
Exercise
- Create a list of words and join them, like the example above.
- Try changing the seperator string from a space to an underscore.
replace()
Python has builtin support for string replacement. A string is a variable that contains text data. If you dont know about strings, you can read more about strings in this article.
Can call the string.replace(old, new) method using the string object. This article demonstrates the replace method.
Not all programming languages have a standard string replace function. Python has a lot of functionality that comes out of the box.
Example
Replace method
Define a string and call the replace() method. The first parameter is the word to search, the second parameter specifies the new value.
The output needs to be saved in the string. If you dont save the output, the string variable will contain the same contents. Saving output is done using: s = function()
Try the program below:
Kod:
[COLOR="palegreen"]s = "Hello World"
s = s.replace("World","Universe")
print(s)[/COLOR]
Save the program as app.py, then run in terminal (or IDE)
python app.py
This will output the new output of string variable s:
Number of words to replace
An optional parameter is the number of items that will be replaced. By default its all.
The program below replaces only the first item:
Kod:
[COLOR="PaleGreen"]s = "Hello World World World"
s = s.replace("World","Universe",1)
print(s)[/COLOR]
The parameter (1) indicates that the string should be replaced only once.
Exercise
Try the replace program
Can a string be replaced twice?
Does replace only work with words or also phrases?
//Quoted. Thanks for reading.