join() and replace() functions in Python

Dolyetyus

Özel Üye
21 Nis 2020
1,207
675
Delft
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 you’d like, you can pass a variable holding the sequence as argument. This makes it easier to read. We’ll 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:

string-join.png


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 don’t 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 don’t 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:

python-replace.png


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.
 
Üst

Turkhackteam.org internet sitesi 5651 sayılı kanun’un 2. maddesinin 1. fıkrasının m) bendi ile aynı kanunun 5. maddesi kapsamında "Yer Sağlayıcı" konumundadır. İçerikler ön onay olmaksızın tamamen kullanıcılar tarafından oluşturulmaktadır. Turkhackteam.org; Yer sağlayıcı olarak, kullanıcılar tarafından oluşturulan içeriği ya da hukuka aykırı paylaşımı kontrol etmekle ya da araştırmakla yükümlü değildir. Türkhackteam saldırı timleri Türk sitelerine hiçbir zararlı faaliyette bulunmaz. Türkhackteam üyelerinin yaptığı bireysel hack faaliyetlerinden Türkhackteam sorumlu değildir. Sitelerinize Türkhackteam ismi kullanılarak hack faaliyetinde bulunulursa, site-sunucu erişim loglarından bu faaliyeti gerçekleştiren ip adresini tespit edip diğer kanıtlarla birlikte savcılığa suç duyurusunda bulununuz.