Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

join two strings python

var1 = "Hello"
var2 = "World"
 
# join() method is used to combine the strings
print("".join([var1, var2]))
 
# join() method is used here to combine
# the string with a separator Space(" ")
var3 = " ".join([var1, var2])
 
print(var3)
Comment

how to combine strings python

# The + operator can be used to concatenate two different strings.

x = "hello "
y = "world!"

# Concatenate two strings
print(x + y)
# OUTPUT: hello world!
Comment

python merge strings

my_list = ['a', 'b', 'c', 'd']
my_string = ','.join(my_list)
# Output = 'a,b,c,d'
Comment

python concatenate strings

x = ‘apples’
y = ‘lemons’
z = “In the basket are %s and %s” % (x,y)
Comment

combining strings in python

# concatenating strings just means combining strings together
# it is used to add one string to the end of another
# below are two exmaples of how concatenation can be used 
# to output 'Hello World':

# example 1:
hello_world = "Hello" + " World"
print(hello_world)

>>> Hello World

# example 2:
hello = "Hello"
print(hello + " World")

>>> Hello World
Comment

concatenate string in python

###############################################################################################
#
# This code snippet takes a string input and append suffixes based on a number of conditions
# Condtion:
#    1 if the lenght of the string is grater than 3 characters and is not already
#       ended with "ing" then add suffix "ing"
#    2 else if the lenght of the string is grater than 3 then add suffix "ly"
#
# The code snippet can be improved to include more business conditions.
################################################################################################


# input string.
s_word = input()

# suffix to add to the input string if the condition is met
# the condition is, the string length must be grater than 3
suffix1 = "ing"

# suffix to add to the input string when the condition is not met
suffix2 = "ly"

# Initialize result to the input string.
# The same input string will be retured if 
# None of the conditions are met.
result = s_word

# check if s_word is all letters
# you don't want to add "ing" or "ly" to string of numbers
if s_word.isalpha():

    # trim all leading or trailing spaces
    s_word = s_word.strip()

    #1 The string contains at least 3 characters/letters
    if len(s_word) >= 3:

        # Extract the last 3 character of the string
        str_end = s_word[-3:].lower()
        print(str_end)
        # Append suffix1 if the last 3 character of the string
        # do not already contains it
        if str_end != suffix1.lower():
            result = s_word+suffix1

        #2 Append suffix2 if the string already ends
        #  with suffix1
        else:
            result = s_word+suffix2

print(result)
Comment

PREVIOUS NEXT
Code Example
Python :: timedelta python days 
Python :: django fieldset 
Python :: dataframe fill nan with mode 
Python :: how to get what type of file a file is in python 
Python :: find word position in string python 
Python :: python string to list new line 
Python :: python remove duplicates 
Python :: reversed python 
Python :: python csv reader cast to float 
Python :: plot multiindex columns pandas 
Python :: python community 
Python :: pandas description of dataframe renaming column values 
Python :: four digit representation python 
Python :: python comparison operators 
Python :: reading a file line by line using a generator 
Python :: functions python examples 
Python :: form action in django 
Python :: python list to dict 
Python :: download folder collab 
Python :: django forms request 
Python :: how to click a div element in selenium python 
Python :: wxpython icon 
Python :: check for string in list python 
Python :: savefig matplotlib python 
Python :: range of y & x in scatter 
Python :: monty hall problem in python 
Python :: pandas series top 5 percent 
Python :: pygame examples 
Python :: np matrix drop zero column 
Python :: pandas dataframe map 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =