Cara menggunakan mix string python


String concatenation means add strings together.

Use the + character to add a variable to another variable:

Example

Merge variable a with variable b into variable c:

a = "Hello"
b = "World"
c = a + b
print(c)

Try it Yourself »

Example

To add a space between them, add a " ":

a = "Hello"
b = "World"
c = a + " " + b
print(c)

Try it Yourself »

For numbers, the + character works as a mathematical operator:

If you try to combine a string and a number, Python will give you an error:




SPLIT: Divides text around a specified character or string, and puts each fragment into a separate cell in the row.

JOIN: Concatenates the elements of one or more one-dimensional arrays using a specified delimiter.

There are few guarantees in life: death, taxes, and programmers needing to deal with strings. Strings can come in many forms. They could be unstructured text, usernames, product descriptions, database column names, or really anything else that we describe using language.

With the near-ubiquity of string data, it’s important to master the tools of the trade when it comes to strings. Luckily, Python makes string manipulation very simple, especially when compared to other languages and even older versions of Python.

In this article, you will learn some of the most fundamental string operations: splitting, concatenating, and joining. Not only will you learn how to use these tools, but you will walk away with a deeper understanding of how they work under the hood.

Take the Quiz: Test your knowledge with our interactive “Splitting, Concatenating, and Joining Strings in Python” quiz. Upon completion you will receive a score so you can track your learning progress over time:

Take the Quiz »

Free Bonus: Click here to get a Python Cheat Sheet and learn the basics of Python 3, like working with data types, dictionaries, lists, and Python functions.

Splitting Strings

In Python, strings are represented as objects, which are immutable: this means that the object as represented in memory can not be directly altered. These two facts can help you learn (and then remember) how to use

# Do this instead:
'a,b,c'.split(',')
9.

Have you guessed how those two features of strings relate to splitting functionality in Python? If you guessed that

# Do this instead:
'a,b,c'.split(',')
9 is an because strings are a special type, you would be correct! In some other languages (like Perl), the original string serves as an input to a standalone
# Do this instead:
'a,b,c'.split(',')
9 function rather than a method called on the string itself.

Note: Ways to Call String Methods

String methods like

# Do this instead:
'a,b,c'.split(',')
9 are mainly shown here as instance methods that are called on strings. They can also be called as static methods, but this isn’t ideal because it’s more “wordy.” For the sake of completeness, here’s an example:

# Avoid this:
str.split('a,b,c', ',')

This is bulky and unwieldy when you compare it to the preferred usage:

# Do this instead:
'a,b,c'.split(',')

For more on instance, class, and static methods in Python, check out our in-depth tutorial.

What about string immutability? This should remind you that string methods are not in-place operations, but they return a new object in memory.

Note: In-Place Operations

In-place operations are operations that directly change the object on which they are called. A common example is the

>>> 'this is my string'.split()
['this', 'is', 'my', 'string']
3 method that is used on lists: when you call
>>> 'this is my string'.split()
['this', 'is', 'my', 'string']
3 on a list, that list is directly changed by adding the input to
>>> 'this is my string'.split()
['this', 'is', 'my', 'string']
3 to the same list.

Remove ads

Splitting Without Parameters

Before going deeper, let’s look at a simple example:

>>>

>>> 'this is my string'.split()
['this', 'is', 'my', 'string']

This is actually a special case of a

# Do this instead:
'a,b,c'.split(',')
9 call, which I chose for its simplicity. Without any separator specified,
# Do this instead:
'a,b,c'.split(',')
9 will count any whitespace as a separator.

Another feature of the bare call to

# Do this instead:
'a,b,c'.split(',')
9 is that it automatically cuts out leading and trailing whitespace, as well as consecutive whitespace. Compare calling
# Do this instead:
'a,b,c'.split(',')
9 on the following string without a separator parameter and with having
>>> s = ' this   is  my string '
>>> s.split()
['this', 'is', 'my', 'string']
>>> s.split(' ')
['', 'this', '', '', 'is', '', 'my', 'string', '']
0 as the separator parameter:

>>>

>>> s = ' this   is  my string '
>>> s.split()
['this', 'is', 'my', 'string']
>>> s.split(' ')
['', 'this', '', '', 'is', '', 'my', 'string', '']

The first thing to notice is that this showcases the immutability of strings in Python: subsequent calls to

# Do this instead:
'a,b,c'.split(',')
9 work on the original string, not on the list result of the first call to
# Do this instead:
'a,b,c'.split(',')
9.

The second—and the main—thing you should see is that the bare

# Do this instead:
'a,b,c'.split(',')
9 call extracts the words in the sentence and discards any whitespace.

Specifying Separators

>>> s = ' this   is  my string '
>>> s.split()
['this', 'is', 'my', 'string']
>>> s.split(' ')
['', 'this', '', '', 'is', '', 'my', 'string', '']
4, on the other hand, is much more literal. When there are leading or trailing separators, you’ll get an empty string, which you can see in the first and last elements of the resulting list.

Where there are multiple consecutive separators (such as between “this” and “is” and between “is” and “my”), the first one will be used as the separator, and the subsequent ones will find their way into your result list as empty strings.

Note: Separators in Calls to

# Do this instead:
'a,b,c'.split(',')
9

While the above example uses a single space character as a separator input to

# Do this instead:
'a,b,c'.split(',')
9, you aren’t limited in the types of characters or length of strings you use as separators. The only requirement is that your separator be a string. You could use anything from
>>> s = ' this   is  my string '
>>> s.split()
['this', 'is', 'my', 'string']
>>> s.split(' ')
['', 'this', '', '', 'is', '', 'my', 'string', '']
7 to even
>>> s = ' this   is  my string '
>>> s.split()
['this', 'is', 'my', 'string']
>>> s.split(' ')
['', 'this', '', '', 'is', '', 'my', 'string', '']
8.

Limiting Splits With Maxsplit

# Do this instead:
'a,b,c'.split(',')
9 has another optional parameter called
>>> s = "this is my string"
>>> s.split(maxsplit=1)
['this', 'is my string']
0. By default,
# Do this instead:
'a,b,c'.split(',')
9 will make all possible splits when called. When you give a value to
>>> s = "this is my string"
>>> s.split(maxsplit=1)
['this', 'is my string']
0, however, only the given number of splits will be made. Using our previous example string, we can see
>>> s = "this is my string"
>>> s.split(maxsplit=1)
['this', 'is my string']
0 in action:

>>>

>>> s = "this is my string"
>>> s.split(maxsplit=1)
['this', 'is my string']

As you see above, if you set

>>> s = "this is my string"
>>> s.split(maxsplit=1)
['this', 'is my string']
0 to
>>> s = "this is my string"
>>> s.split(maxsplit=1)
['this', 'is my string']
5, the first whitespace region is used as the separator, and the rest are ignored. Let’s do some exercises to test out everything we’ve learned so far.

Exercise: "Try It Yourself: Maxsplit"Show/Hide

What happens when you give a negative number as the

>>> s = "this is my string"
>>> s.split(maxsplit=1)
['this', 'is my string']
0 parameter?

Solution: "Try It Yourself: Maxsplit"Show/Hide

# Do this instead:
'a,b,c'.split(',')
9 will split your string on all available separators, which is also the default behavior when
>>> s = "this is my string"
>>> s.split(maxsplit=1)
['this', 'is my string']
0 isn’t set.

Exercise: "Section Comprehension Check"Show/Hide

You were recently handed a comma-separated value (CSV) file that was horribly formatted. Your job is to extract each row into an list, with each element of that list representing the columns of that file. What makes it badly formatted? The “address” field includes multiple commas but needs to be represented in the list as a single element!

Assume that your file has been loaded into memory as the following multiline string:

Name,Phone,Address
Mike Smith,15554218841,123 Nice St, Roy, NM, USA
Anita Hernandez,15557789941,425 Sunny St, New York, NY, USA
Guido van Rossum,315558730,Science Park 123, 1098 XG Amsterdam, NL

Your output should be a list of lists:

[
    ['Mike Smith', '15554218841', '123 Nice St, Roy, NM, USA'],
    ['Anita Hernandez', '15557789941', '425 Sunny St, New York, NY, USA'],
    ['Guido van Rossum', '315558730', 'Science Park 123, 1098 XG Amsterdam, NL']
]

Each inner list represents the rows of the CSV that we’re interested in, while the outer list holds it all together.

Solution: "Section Comprehension Check"Show/Hide

Here’s my solution. There are a few ways to attack this. The important thing is that you used

# Do this instead:
'a,b,c'.split(',')
9 with all its optional parameters and got the expected output:

input_string = """Name,Phone,Address
Mike Smith,15554218841,123 Nice St, Roy, NM, USA
Anita Hernandez,15557789941,425 Sunny St, New York, NY, USA
Guido van Rossum,315558730,Science Park 123, 1098 XG Amsterdam, NL"""

def string_split_ex(unsplit):
    results = []

    # Bonus points for using splitlines() here instead, 
    # which will be more readable
    for line in unsplit.split('\n')[1:]:
        results.append(line.split(',', maxsplit=2))

    return results

print(string_split_ex(input_string))

We call

# Do this instead:
'a,b,c'.split(',')
9 twice here. The first usage can look intimidating, but don’t worry! We’ll step through it, and you’ll get comfortable with expressions like these. Let’s take another look at the first
# Do this instead:
'a,b,c'.split(',')
9 call:
Name,Phone,Address
Mike Smith,15554218841,123 Nice St, Roy, NM, USA
Anita Hernandez,15557789941,425 Sunny St, New York, NY, USA
Guido van Rossum,315558730,Science Park 123, 1098 XG Amsterdam, NL
2.

The first element is

Name,Phone,Address
Mike Smith,15554218841,123 Nice St, Roy, NM, USA
Anita Hernandez,15557789941,425 Sunny St, New York, NY, USA
Guido van Rossum,315558730,Science Park 123, 1098 XG Amsterdam, NL
3, which is just the variable that points to your input string. Then we have our
# Do this instead:
'a,b,c'.split(',')
9 call:
Name,Phone,Address
Mike Smith,15554218841,123 Nice St, Roy, NM, USA
Anita Hernandez,15557789941,425 Sunny St, New York, NY, USA
Guido van Rossum,315558730,Science Park 123, 1098 XG Amsterdam, NL
5. Here, we are splitting on a special character called the newline character.

What does

Name,Phone,Address
Mike Smith,15554218841,123 Nice St, Roy, NM, USA
Anita Hernandez,15557789941,425 Sunny St, New York, NY, USA
Guido van Rossum,315558730,Science Park 123, 1098 XG Amsterdam, NL
6 do? As the name implies, it tells whatever is reading the string that every character after it should be shown on the next line. In a multiline string like our
Name,Phone,Address
Mike Smith,15554218841,123 Nice St, Roy, NM, USA
Anita Hernandez,15557789941,425 Sunny St, New York, NY, USA
Guido van Rossum,315558730,Science Park 123, 1098 XG Amsterdam, NL
7, there is a hidden
Name,Phone,Address
Mike Smith,15554218841,123 Nice St, Roy, NM, USA
Anita Hernandez,15557789941,425 Sunny St, New York, NY, USA
Guido van Rossum,315558730,Science Park 123, 1098 XG Amsterdam, NL
6 at the end of each line.

The final part might be new:

Name,Phone,Address
Mike Smith,15554218841,123 Nice St, Roy, NM, USA
Anita Hernandez,15557789941,425 Sunny St, New York, NY, USA
Guido van Rossum,315558730,Science Park 123, 1098 XG Amsterdam, NL
9. The statement so far gives us a new list in memory, and
Name,Phone,Address
Mike Smith,15554218841,123 Nice St, Roy, NM, USA
Anita Hernandez,15557789941,425 Sunny St, New York, NY, USA
Guido van Rossum,315558730,Science Park 123, 1098 XG Amsterdam, NL
9 looks like a list index notation, and it is—kind of! This extended index notation gives us a list slice. In this case, we take the element at index
>>> s = "this is my string"
>>> s.split(maxsplit=1)
['this', 'is my string']
5 and everything after it, discarding the element at index
[
    ['Mike Smith', '15554218841', '123 Nice St, Roy, NM, USA'],
    ['Anita Hernandez', '15557789941', '425 Sunny St, New York, NY, USA'],
    ['Guido van Rossum', '315558730', 'Science Park 123, 1098 XG Amsterdam, NL']
]
2.

In all, we iterate through a list of strings, where each element represents each line in the multiline input string except for the very first line.

At each string, we call

# Do this instead:
'a,b,c'.split(',')
9 again using
[
    ['Mike Smith', '15554218841', '123 Nice St, Roy, NM, USA'],
    ['Anita Hernandez', '15557789941', '425 Sunny St, New York, NY, USA'],
    ['Guido van Rossum', '315558730', 'Science Park 123, 1098 XG Amsterdam, NL']
]
4 as the split character, but this time we are using
>>> s = "this is my string"
>>> s.split(maxsplit=1)
['this', 'is my string']
0 to only split on the first two commas, leaving the address intact. We then append the result of that call to the aptly named
[
    ['Mike Smith', '15554218841', '123 Nice St, Roy, NM, USA'],
    ['Anita Hernandez', '15557789941', '425 Sunny St, New York, NY, USA'],
    ['Guido van Rossum', '315558730', 'Science Park 123, 1098 XG Amsterdam, NL']
]
6 array and return it to the caller.

Remove ads

Concatenating and Joining Strings

The other fundamental string operation is the opposite of splitting strings: string concatenation. If you haven’t seen this word, don’t worry. It’s just a fancy way of saying “gluing together.”

Concatenating With the [ ['Mike Smith', '15554218841', '123 Nice St, Roy, NM, USA'], ['Anita Hernandez', '15557789941', '425 Sunny St, New York, NY, USA'], ['Guido van Rossum', '315558730', 'Science Park 123, 1098 XG Amsterdam, NL'] ] 7 Operator

There are a few ways of doing this, depending on what you’re trying to achieve. The simplest and most common method is to use the plus symbol (

[
    ['Mike Smith', '15554218841', '123 Nice St, Roy, NM, USA'],
    ['Anita Hernandez', '15557789941', '425 Sunny St, New York, NY, USA'],
    ['Guido van Rossum', '315558730', 'Science Park 123, 1098 XG Amsterdam, NL']
]
7) to add multiple strings together. Simply place a
[
    ['Mike Smith', '15554218841', '123 Nice St, Roy, NM, USA'],
    ['Anita Hernandez', '15557789941', '425 Sunny St, New York, NY, USA'],
    ['Guido van Rossum', '315558730', 'Science Park 123, 1098 XG Amsterdam, NL']
]
7 between as many strings as you want to join together:

>>>

>>> 'a' + 'b' + 'c'
'abc'

In keeping with the math theme, you can also multiply a string to repeat it:

>>>

>>> 'do' * 2
'dodo'

Remember, strings are immutable! If you concatenate or repeat a string stored in a variable, you will have to assign the new string to another variable in order to keep it.

>>>

# Do this instead:
'a,b,c'.split(',')
0

If we didn’t have immutable strings,

input_string = """Name,Phone,Address
Mike Smith,15554218841,123 Nice St, Roy, NM, USA
Anita Hernandez,15557789941,425 Sunny St, New York, NY, USA
Guido van Rossum,315558730,Science Park 123, 1098 XG Amsterdam, NL"""

def string_split_ex(unsplit):
    results = []

    # Bonus points for using splitlines() here instead, 
    # which will be more readable
    for line in unsplit.split('\n')[1:]:
        results.append(line.split(',', maxsplit=2))

    return results

print(string_split_ex(input_string))
0 would instead output
input_string = """Name,Phone,Address
Mike Smith,15554218841,123 Nice St, Roy, NM, USA
Anita Hernandez,15557789941,425 Sunny St, New York, NY, USA
Guido van Rossum,315558730,Science Park 123, 1098 XG Amsterdam, NL"""

def string_split_ex(unsplit):
    results = []

    # Bonus points for using splitlines() here instead, 
    # which will be more readable
    for line in unsplit.split('\n')[1:]:
        results.append(line.split(',', maxsplit=2))

    return results

print(string_split_ex(input_string))
1.

Another note is that Python does not do implicit string conversion. If you try to concatenate a string with a non-string type, Python will raise a

input_string = """Name,Phone,Address
Mike Smith,15554218841,123 Nice St, Roy, NM, USA
Anita Hernandez,15557789941,425 Sunny St, New York, NY, USA
Guido van Rossum,315558730,Science Park 123, 1098 XG Amsterdam, NL"""

def string_split_ex(unsplit):
    results = []

    # Bonus points for using splitlines() here instead, 
    # which will be more readable
    for line in unsplit.split('\n')[1:]:
        results.append(line.split(',', maxsplit=2))

    return results

print(string_split_ex(input_string))
2:

>>>

# Do this instead:
'a,b,c'.split(',')
1

This is because you can only concatenate strings with other strings, which may be new behavior for you if you’re coming from a language like JavaScript, which attempts to do implicit type conversion.

Going From a List to a String in Python With input_string = """Name,Phone,Address Mike Smith,15554218841,123 Nice St, Roy, NM, USA Anita Hernandez,15557789941,425 Sunny St, New York, NY, USA Guido van Rossum,315558730,Science Park 123, 1098 XG Amsterdam, NL""" def string_split_ex(unsplit): results = [] # Bonus points for using splitlines() here instead, # which will be more readable for line in unsplit.split('\n')[1:]: results.append(line.split(',', maxsplit=2)) return results print(string_split_ex(input_string)) 3

There is another, more powerful, way to join strings together. You can go from a list to a string in Python with the

input_string = """Name,Phone,Address
Mike Smith,15554218841,123 Nice St, Roy, NM, USA
Anita Hernandez,15557789941,425 Sunny St, New York, NY, USA
Guido van Rossum,315558730,Science Park 123, 1098 XG Amsterdam, NL"""

def string_split_ex(unsplit):
    results = []

    # Bonus points for using splitlines() here instead, 
    # which will be more readable
    for line in unsplit.split('\n')[1:]:
        results.append(line.split(',', maxsplit=2))

    return results

print(string_split_ex(input_string))
4 method.

The common use case here is when you have an iterable—like a list—made up of strings, and you want to combine those strings into a single string. Like

# Do this instead:
'a,b,c'.split(',')
9,
input_string = """Name,Phone,Address
Mike Smith,15554218841,123 Nice St, Roy, NM, USA
Anita Hernandez,15557789941,425 Sunny St, New York, NY, USA
Guido van Rossum,315558730,Science Park 123, 1098 XG Amsterdam, NL"""

def string_split_ex(unsplit):
    results = []

    # Bonus points for using splitlines() here instead, 
    # which will be more readable
    for line in unsplit.split('\n')[1:]:
        results.append(line.split(',', maxsplit=2))

    return results

print(string_split_ex(input_string))
3 is a string instance method. If all of your strings are in an iterable, which one do you call
input_string = """Name,Phone,Address
Mike Smith,15554218841,123 Nice St, Roy, NM, USA
Anita Hernandez,15557789941,425 Sunny St, New York, NY, USA
Guido van Rossum,315558730,Science Park 123, 1098 XG Amsterdam, NL"""

def string_split_ex(unsplit):
    results = []

    # Bonus points for using splitlines() here instead, 
    # which will be more readable
    for line in unsplit.split('\n')[1:]:
        results.append(line.split(',', maxsplit=2))

    return results

print(string_split_ex(input_string))
3 on?

This is a bit of a trick question. Remember that when you use

# Do this instead:
'a,b,c'.split(',')
9, you call it on the string or character you want to split on. The opposite operation is
input_string = """Name,Phone,Address
Mike Smith,15554218841,123 Nice St, Roy, NM, USA
Anita Hernandez,15557789941,425 Sunny St, New York, NY, USA
Guido van Rossum,315558730,Science Park 123, 1098 XG Amsterdam, NL"""

def string_split_ex(unsplit):
    results = []

    # Bonus points for using splitlines() here instead, 
    # which will be more readable
    for line in unsplit.split('\n')[1:]:
        results.append(line.split(',', maxsplit=2))

    return results

print(string_split_ex(input_string))
3, so you call it on the string or character you want to use to join your iterable of strings together:

>>>

# Do this instead:
'a,b,c'.split(',')
2

Here, we join each element of the

>>> 'a' + 'b' + 'c'
'abc'
0 list with a comma (
[
    ['Mike Smith', '15554218841', '123 Nice St, Roy, NM, USA'],
    ['Anita Hernandez', '15557789941', '425 Sunny St, New York, NY, USA'],
    ['Guido van Rossum', '315558730', 'Science Park 123, 1098 XG Amsterdam, NL']
]
4) and call
input_string = """Name,Phone,Address
Mike Smith,15554218841,123 Nice St, Roy, NM, USA
Anita Hernandez,15557789941,425 Sunny St, New York, NY, USA
Guido van Rossum,315558730,Science Park 123, 1098 XG Amsterdam, NL"""

def string_split_ex(unsplit):
    results = []

    # Bonus points for using splitlines() here instead, 
    # which will be more readable
    for line in unsplit.split('\n')[1:]:
        results.append(line.split(',', maxsplit=2))

    return results

print(string_split_ex(input_string))
3 on it rather than the
>>> 'a' + 'b' + 'c'
'abc'
0 list.

Exercise: "Readability Improvement with Join"Show/Hide

How could you make the output text more readable?

Solution: "Readability Improvement with Join"Show/Hide

One thing you could do is add spacing:

>>>

# Do this instead:
'a,b,c'.split(',')
3

By doing nothing more than adding a space to our join string, we’ve vastly improved the readability of our output. This is something you should always keep in mind when joining strings for human readability.

input_string = """Name,Phone,Address
Mike Smith,15554218841,123 Nice St, Roy, NM, USA
Anita Hernandez,15557789941,425 Sunny St, New York, NY, USA
Guido van Rossum,315558730,Science Park 123, 1098 XG Amsterdam, NL"""

def string_split_ex(unsplit):
    results = []

    # Bonus points for using splitlines() here instead, 
    # which will be more readable
    for line in unsplit.split('\n')[1:]:
        results.append(line.split(',', maxsplit=2))

    return results

print(string_split_ex(input_string))
3 is smart in that it inserts your “joiner” in between the strings in the iterable you want to join, rather than just adding your joiner at the end of every string in the iterable. This means that if you pass an iterable of size
>>> s = "this is my string"
>>> s.split(maxsplit=1)
['this', 'is my string']
5, you won’t see your joiner:

>>>

# Do this instead:
'a,b,c'.split(',')
4

Exercise: "Section Comprehension Check"Show/Hide

Using our web scraping tutorial, you’ve built a great weather scraper. However, it loads string information in a list of lists, each holding a unique row of information you want to write out to a CSV file:

# Do this instead:
'a,b,c'.split(',')
5

Your output should be a single string that looks like this:

# Do this instead:
'a,b,c'.split(',')
6

Solution: "Section Comprehension Check"Show/Hide

For this solution, I used a list comprehension, which is a powerful feature of Python that allows you to rapidly build lists. If you want to learn more about them, check out this great article that covers all the comprehensions available in Python.

Below is my solution, starting with a list of lists and ending with a single string:

# Do this instead:
'a,b,c'.split(',')
7

Here we use

input_string = """Name,Phone,Address
Mike Smith,15554218841,123 Nice St, Roy, NM, USA
Anita Hernandez,15557789941,425 Sunny St, New York, NY, USA
Guido van Rossum,315558730,Science Park 123, 1098 XG Amsterdam, NL"""

def string_split_ex(unsplit):
    results = []

    # Bonus points for using splitlines() here instead, 
    # which will be more readable
    for line in unsplit.split('\n')[1:]:
        results.append(line.split(',', maxsplit=2))

    return results

print(string_split_ex(input_string))
3 not once, but twice. First, we use it in the list comprehension, which does the work of combining all the strings in each inner list into a single string. Next, we join each of these strings with the newline character
Name,Phone,Address
Mike Smith,15554218841,123 Nice St, Roy, NM, USA
Anita Hernandez,15557789941,425 Sunny St, New York, NY, USA
Guido van Rossum,315558730,Science Park 123, 1098 XG Amsterdam, NL
6 that we saw earlier. Finally, we simply print the result so we can verify that it is as we expected.

Remove ads

Tying It All Together

While this concludes this overview of the most basic string operations in Python (splitting, concatenating, and joining), there is still a whole universe of string methods that can make your experiences with manipulating strings much easier.

Once you have mastered these basic string operations, you may want to learn more. Luckily, we have a number of great tutorials to help you complete your mastery of Python’s features that enable smart string manipulation:

  • Python 3’s f-Strings: An Improved String Formatting Syntax
  • Python String Formatting Best Practices
  • Strings and Character Data in Python

Take the Quiz: Test your knowledge with our interactive “Splitting, Concatenating, and Joining Strings in Python” quiz. Upon completion you will receive a score so you can track your learning progress over time:

Take the Quiz »

Mark as Completed

Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Splitting, Concatenating, and Joining Strings in Python

🐍 Python Tricks 💌

Get a short & sweet Python Trick delivered to your inbox every couple of days. No spam ever. Unsubscribe any time. Curated by the Real Python team.

Cara menggunakan mix string python

Send Me Python Tricks »

About Kyle Stratis

Cara menggunakan mix string python
Cara menggunakan mix string python

Kyle is a self-taught developer working as a senior data engineer at Vizit Labs. In the past, he has founded DanqEx (formerly Nasdanq: the original meme stock exchange) and Encryptid Gaming.

» More about Kyle


Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:

Cara menggunakan mix string python

Adriana

Cara menggunakan mix string python

Brad

Cara menggunakan mix string python

Dan

Cara menggunakan mix string python

Joanna

Master Real-World Python Skills With Unlimited Access to Real Python

Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:

Level Up Your Python Skills »

Master Real-World Python Skills
With Unlimited Access to Real Python

Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:

Level Up Your Python Skills »

What Do You Think?

Rate this article:

Tweet Share Share Email

What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment below and let us know.

Commenting Tips: The most useful comments are those written with the goal of learning from or helping out other students. and get answers to common questions in our support portal.

Apa itu %f pada Python?

Tanda %s akan otomatis diganti dengan nilai yang kita inputkan ke variabel nama . Tanda %s untuk tipe data teks, %d untuk angka (desimal), dan %f untuk bilangan pecahan.

Apakah string dapat digunakan pada Python?

String dalam python adalah bytes array yang mempresentasikan unicode char. Python tidak punya tipe data char, sehingga char pada python diganti dengan string yang punya panjang satu karakter. Dalam mengakses elemen pada string menggunakan brackets atau kurung siku [].

Apa yang dimaksud dengan array of string?

Adalah sekelompok data yang sejenis yang disimpan didalam memori secara berurutan dengan sebuah nama variable, dan untuk membedakan antara 1 data dengan data yang lainnya digunakan index. Untuk menginisialisasi array, elemen-elemen array diletakkan diantara tanda kurung.

Disebut apa teknik untuk membuat potongan string?

Slicing String Untuk melakukan slicing atau pemotongan string, kita bisa menggunakan range of index yang diapit oleh dua kurung siku ( [] ) dan dipisahkan oleh tanda titik dua ( : ).