--- title: 'Python Challenge Day 5 - Remove String Spaces' disqus: hackmd --- ## Python Challenge Day 5 - Remove String Spaces ### DESCRIPTION Write a function that removes the spaces from the string, then return the resultant string. ### Examples: ```gherkin= Input -> Output "8 j 8 mBliB8g imjB8B8 jl B" -> "8j8mBliB8gimjB8B8jlB" "8 8 Bi fk8h B 8 BB8B B B B888 c hl8 BhB fd" -> "88Bifk8hB8BB8BBBB888chl8BhBfd" "8aaaaa dddd r " -> "8aaaaaddddr" ``` 程式邏輯 --- 目的:移除字串的空白字元 1. 使用split 和 join 方法替換空格 Solution --- ``` python def no_space(x): x = ''.join(x.split()) return x ``` * split 函數可以根據空格拆分字符串為數組,join 函數把數字串組合併為字符串 * <string>.split(sep,maxsplit) <string> 是任何有效的字符串 sep 是用來當作拆分的分隔符號,是一個可選參數,預設值是把空格當作拆分的分隔符號 maxsplit 是可選參數,要拆分string的次數,預設值是-1 * <sep>.join(<iterable>) <iterable> 任何字串 <sep> 想要分隔的符號 ## 參考資源 :::info * [Kata](https://www.codewars.com/kata/57eae20f5500ad98e50002c5) :::
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up